passing proc blocks to rhtml templates

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 Ham

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('</form>', 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.

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

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:

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

http://errtheblog.com/post/13

Perfect. Thanks

Zack