Rails: Commuincating between action level layout and global layout
Today I learned a cool new trick.
Lets say you have a controller named blog_controller.rb that uses "global" layout named default:
layout 'default'
And you have a views/blog/index.rhtml template (action level layout)
then you can communicate between the two using "content_for" variables:
For example, I can create a variable called @content_for_my_javascripts in views/blog/index.rhtml using
<% content_for("my_javascripts") do -%>
# javascript code here
<% end -%>
Now I can place the following in my global template (views/layout/default.rhtml) to put these javascripts there
<script type="text/javascript"><%= @content_for_my_javascripts %></script>
It seems that "action level layout templates" are rendered before the "global layout templates"
Cool eh
1 Comments:
Thanks, this was very useful... I'd imagine this coming in very handy for a CMS system...
Post a Comment
<< Home