# Yeilding Snippets

**URL:** <https://www.ruby-forum.com/t/yeilding-snippets/128833>\
**Category:** Radiant CMS\
**Created:** [February 12, 2008, 12:34am UTC](https://www.ruby-forum.com/t/yeilding-snippets/128833 "2008-02-12T00:34:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrewneal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@andrewneal](https://www.ruby-forum.com/u/andrewneal)\
**Post date:** [February 12, 2008, 12:34am UTC](https://www.ruby-forum.com/t/yeilding-snippets/128833/1 "2008-02-12T00:34:56Z")

</div>

I would like to share an idea that I thought would make snippets even  
more useful.

Rather than always calling a snippet as a single, self-closing tag:

\<r:snippet name=“my-snip”/\>

You could call it as a pair of tags, with content in between:

\<r:snippet name=“my-snip”\>

Lorem ipsum dolor sit amet…

  
\</r:snippet\>

The snippet itself could optionally call \<r:yeild/\>. If this tag  
occurs anywhere in the snippet, it would be replaced with the content  
appearing between the open/close tags which call the snippet.

An example:

In this case, the `<r:snippet>` opening tag would correspond to the  
four opening div tags, and the `</r:snippet>` closing tag would  
correspond to the four closing div tags.

I have thought about achieving the same markup by writing two separate  
snippets, and calling from my layout:

\<r:snippet name=“open-four-divs”\>

Lorem ipsum dolor sit amet…

  
\<r:snippet name=“close-four-divs”\>

But it just looks and feels so wrong!

I’d like to hear what you think about the idea. Could you see a use  
for it? Would it be difficult to implement? Does Radiant already do  
this, and I just didn’t realise?

Cheers,

Drew

---

<div class="post-metadata">

**Author:** ![andrewneal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@andrewneal](https://www.ruby-forum.com/u/andrewneal)\
**Post date:** [February 12, 2008, 5:17am UTC](https://www.ruby-forum.com/t/yeilding-snippets/128833/2 "2008-02-12T05:17:03Z")

</div>

Andrew,

You would need to capture the contents of the \<r:snippet /\> tag before  
rendering the snippet, either using tag.block or tag.expand and saving  
it in a local. Then your \<r:yield /\> tag would emit that local.

Sean

---

<div class="post-metadata">

**Author:** ![andrewneal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@andrewneal](https://www.ruby-forum.com/u/andrewneal)\
**Post date:** [February 14, 2008, 1:00am UTC](https://www.ruby-forum.com/t/yeilding-snippets/128833/3 "2008-02-14T01:00:00Z")

</div>

Sean, thanks for your reply. I thought there would be more to it than  
that, but you made it sound easy, so I gave it a go. Here is the ticket:

[http://dev.radiantcms.org/radiant/ticket/611](http://dev.radiantcms.org/radiant/ticket/611)  
[http://dev.radiantcms.org/radiant/attachment/ticket/611/enable\_snippets\_as\_double\_tag\_with\_yield.diff](http://dev.radiantcms.org/radiant/attachment/ticket/611/enable_snippets_as_double_tag_with_yield.diff)

> You would need to capture the contents of the \<r:snippet /\> tag before  
> rendering the snippet, either using tag.block or tag.expand and saving  
> it in a local. Then your \<r:yield /\> tag would emit that local.

I wasn’t sure how to save tag.expand in a local so that it was  
accessible from \<r:yield/\>, so I’ve cheated a little:

snippet.content.gsub!(%r{\<r:yield\s?/?\>}, tag.expand)

and making \<r:yield/\> output nothing. It’s not exactly idiomatic, but  
it passes the tests!

Cheers,

Drew

---

<div class="post-metadata">

**Author:** ![andrewneal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@andrewneal](https://www.ruby-forum.com/u/andrewneal)\
**Post date:** [February 15, 2008, 1:18am UTC](https://www.ruby-forum.com/t/yeilding-snippets/128833/4 "2008-02-15T01:18:53Z")

</div>

Thanks Sean, updated my patch:

[http://dev.radiantcms.org/radiant/attachment/ticket/611/enable\_snippets\_as\_double\_tag\_with\_yield.2.diff](http://dev.radiantcms.org/radiant/attachment/ticket/611/enable_snippets_as_double_tag_with_yield.2.diff)

Cheers,

Drew

---

<div class="post-metadata">

**Author:** ![andrewneal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4491bb/32.png) [@andrewneal](https://www.ruby-forum.com/u/andrewneal)\
**Post date:** [February 14, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/yeilding-snippets/128833/5 "2008-02-14T15:22:06Z")

</div>

Something like this:

(in your snippet tag)

tag.locals.yield = tag.expand

tag ‘yield’ do |tag|  
tag.locals.yield  
end
