# Re-usable template pattern - yield?

**URL:** <https://www.ruby-forum.com/t/re-usable-template-pattern-yield/192215>\
**Category:** Rails\
**Created:** [July 26, 2010, 2:55pm UTC](https://www.ruby-forum.com/t/re-usable-template-pattern-yield/192215 "2010-07-26T14:55:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 26, 2010, 2:55pm UTC](https://www.ruby-forum.com/t/re-usable-template-pattern-yield/192215/1 "2010-07-26T14:55:59Z")

</div>

Hi all. I have something that’s like a helper and like a partial but  
not quite either. Instead of typing this:

\<% if (page = ComatosePage.find\_by\_path(“general/explore”)) ||  
current\_user.admin? %\>

  

  
\<%= page ? page.to\_html : “Comatose article ‘general/explore’  
not found” %\>  

  

  
\<% end %\>

I want to do something like this but i can’t get my head around quite  
how to set it up:

\<% comatose\_article(“general/explore”) do |article| %\>

  

  
\<%= article %\>  

  

  
\<% end %\>

This is so i can use the same pattern of “if the article’s not there  
show nothing at all, unless the current user is an admin in which case  
show the ‘missing article’ message”, but with arbitrary html wrapped  
around the actual article (in this case the arbitrary html is the  
quarter and module divs)

grateful for any advice - max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 26, 2010, 3:20pm UTC](https://www.ruby-forum.com/t/re-usable-template-pattern-yield/192215/2 "2010-07-26T15:20:48Z")

</div>

On Jul 26, 1:55 pm, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is so i can use the same pattern of “if the article’s not there  
> show nothing at all, unless the current user is an admin in which case  
> show the ‘missing article’ message”, but with arbitrary html wrapped  
> around the actual article (in this case the arbitrary html is the  
> quarter and module divs)

A helper that yields is what you want. Have a look at the concat &  
capture helpers too.

Fred

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 26, 2010, 4:00pm UTC](https://www.ruby-forum.com/t/re-usable-template-pattern-yield/192215/3 "2010-07-26T16:00:13Z")

</div>

Frederick C. wrote:

> On Jul 26, 1:55ï¿½pm, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > This is so i can use the same pattern of “if the article’s not there  
> > show nothing at all, unless the current user is an admin in which case  
> > show the ‘missing article’ message”, but with arbitrary html wrapped  
> > around the actual article (in this case the arbitrary html is the  
> > quarter and module divs)
> 
> A helper that yields is what you want. Have a look at the concat &  
> capture helpers too.
> 
> Fred

Thanks for the prod Fred. I knew it would be something using yield but  
couldn’t quite get my head around it. It turned out to be simpler than  
i thought:

def comatose\_article(path)  
if page = ComatosePage.find\_by\_path(path)  
yield page.to\_html  
elsif current\_user.admin?  
yield “Couldn’t find ‘#{path}’ page”  
end  
end

Cheers, max
