# Passing proc blocks to rhtml templates

**URL:** <https://www.ruby-forum.com/t/passing-proc-blocks-to-rhtml-templates/77594>\
**Category:** Rails\
**Created:** [November 29, 2006, 12:25am UTC](https://www.ruby-forum.com/t/passing-proc-blocks-to-rhtml-templates/77594 "2006-11-29T00:25:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zackola](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@zackola](https://www.ruby-forum.com/u/zackola)\
**Post date:** [November 29, 2006, 12:25am UTC](https://www.ruby-forum.com/t/passing-proc-blocks-to-rhtml-templates/77594/1 "2006-11-29T00:25:12Z")

</div>

I want to create layout components that function like my layout does.  
A  
layout can yield the body content, is there any way to get this  
functionality in rhtml templates? For example, say I have a layout  
component that has a title bar and a content area. If it is a box with  
rounded corners I do not want to repeat this html everywhere, so I’ll  
make a  
helper. Here is what I have to do now:

\<%= start\_mini\_window(“Log in!”) %\>  
log in form html here  
\<%= end\_mini\_window %\>

Is it possible to do this?

\<% mini\_window(“Log in!”) do %\>  
log in form html here  
\<% end %\>

This feels cleaner to me, but I was unable to get it working. I stopped  
trying to get this working when I realized that render() only uses your  
proc  
block when creating a javascriptgenerator.

Any ideas?

Thanks,  
Zack H.

---

<div class="post-metadata">

**Author:** ![zackola](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@zackola](https://www.ruby-forum.com/u/zackola)\
**Post date:** [November 29, 2006, 12:37am UTC](https://www.ruby-forum.com/t/passing-proc-blocks-to-rhtml-templates/77594/2 "2006-11-29T00:37:16Z")

</div>

On 11/29/06, Zack H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%= end\_mini\_window %\>
> 
> Any ideas?
> 
> Thanks,  
> Zack H.

I’m not sure that I can help you with your particular case, but the  
form\_for  
helper works like this.

The code for that method is

```
  def form_for(object_name, *args, &proc)
    raise ArgumentError, "Missing block" unless block_given?
    options = args.last.is_a?(Hash) ? args.pop : {}
    concat(form_tag(options.delete(:url) || {},

```

options.delete(:html) || {}), proc.binding)  
fields\_for(object\_name, \*(args \<\< options), &proc)  
concat(‘’, proc.binding)  
end

This can be found at  
[http://dev.rubyonrails.org/svn/rails/trunk/actionpack/lib/action\_view/helpers/form\_helper.rb](http://dev.rubyonrails.org/svn/rails/trunk/actionpack/lib/action_view/helpers/form_helper.rb)

That might give you some ideas.

---

<div class="post-metadata">

**Author:** ![zackola](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@zackola](https://www.ruby-forum.com/u/zackola)\
**Post date:** [November 29, 2006, 12:47am UTC](https://www.ruby-forum.com/t/passing-proc-blocks-to-rhtml-templates/77594/3 "2006-11-29T00:47:17Z")

</div>

I looked at form\_for because it is a good example of the benefits to  
proc  
blocks in templates. The code didn’t tell me a whole lot unfortunately.  
What I really want to do is take the proc block contents and yield it  
into a  
string, then pass that string to a partial template. I didn’t see  
form\_for  
doing that, but maybe I missed something.

If I can yield the proc block contents into a string, that lets me  
actually  
pass parameters to the block and derive benefits like form\_for does.  
Can  
anyone explain how/if this is possible, and how it would work with  
concat()?

Thanks!  
Zack

---

<div class="post-metadata">

**Author:** ![zackola](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@zackola](https://www.ruby-forum.com/u/zackola)\
**Post date:** [November 29, 2006, 12:46pm UTC](https://www.ruby-forum.com/t/passing-proc-blocks-to-rhtml-templates/77594/4 "2006-11-29T12:46:49Z")

</div>

I think this article at “Err the Blog” has the solution you are looking  
for:

> **[Colored Tests — err.the\_blog](http://errtheblog.com/post/13)**
>
> Err the Blog: Ruby and Rails from the front.

---

<div class="post-metadata">

**Author:** ![zackola](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@zackola](https://www.ruby-forum.com/u/zackola)\
**Post date:** [November 29, 2006, 8:20pm UTC](https://www.ruby-forum.com/t/passing-proc-blocks-to-rhtml-templates/77594/5 "2006-11-29T20:20:44Z")

</div>

Perfect. Thanks

Zack

---

<div class="post-metadata">

**Author:** ![zackola](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@zackola](https://www.ruby-forum.com/u/zackola)\
**Post date:** [November 29, 2006, 6:34am UTC](https://www.ruby-forum.com/t/passing-proc-blocks-to-rhtml-templates/77594/6 "2006-11-29T06:34:07Z")

</div>

copy paste from form\_for() method. you may have to figure out how to  
pass actual params.  
concat() just append strings to output buffer string.

## in your controller helper:

def widget(&proc)  
raise ArgumentError, “Missing block” unless block\_given?  
concat(‘

hello world

’, proc.binding)  
yield proc  
concat(‘
’, proc.binding)  
end

## in rhtml

\<% widget do |w| %\>  
does it work?  
\<% end %\>

## and you got

hello world

does it work?

wuyaSea operator  
[http://www.wuyaSea.com](http://www.wuyaSea.com)
