Hi, I understand that the layout concept is very similar to OpenSymphony''s Sitemesh project. I have done a lot of development with Sitemesh, but am fairly new to Rails. I am wondering if there is a way for a layout in rails to find out value of a variable from a page it is decorating? In sitemesh you would be able to do <meta name="show_menu" content="true"> on some page being decorated and then in the layout page, you can access the value of show_menu using page.getProperty(..) and can show or hide the menu. Is there a way to do something like the above in Rails? For example a template t1.rhtml could be: <div> Some content <!-- can I set a variable show_menu here --> </div> And this is the layout1.rhtml (layout file that t1 gets decorated with): <html> <head></head> <body> <!-- can I use the variable show_menu here that might be defined in the template this file is decorating --> <div id=''menu''></div> <div id=''main''> <%=@content_for_layout%> </div> </body> </html> Is this possible in rails? If yes, how? Thanks, Mufaddal.
Hi, I understand that the layout concept is very similar to OpenSymphony''s Sitemesh project. I have done a lot of development with Sitemesh, but am fairly new to Rails. I am wondering if there is a way for a layout in rails to find out value of a variable from a page it is decorating? In sitemesh you would be able to do <meta name="show_menu" content="true"> on some page being decorated and then in the layout page, you can access the value of show_menu using page.getProperty(..) and can show or hide the menu. Is there a way to do something like the above in Rails? For example a template t1.rhtml could be: <div> Some content <!-- can I set a variable show_menu here --> </div> And this is the layout1.rhtml (layout file that t1 gets decorated with): <html> <head></head> <body> <!-- can I use the variable show_menu here that might be defined in the template this file is decorating --> <div id=''menu''></div> <div id=''main''> <%=@content_for_layout%> </div> </body> </html> Is this possible in rails? If yes, how? Thanks, Mufaddal.
Mufaddal Khumri wrote:> Hi, > > I understand that the layout concept is very similar to OpenSymphony''s > Sitemesh project. I have done a lot of development with Sitemesh, but am > fairly new to Rails. I am wondering if there is a way for a layout in > rails to find out value of a variable from a page it is decorating?Yes - the content is processed before it is passed to the layout, and you can set instance variables (@xxx) in the content for use in the layout.> > In sitemesh you would be able to do <meta name="show_menu" > content="true"> on some page being decorated and then in the layout > page, you can access the value of show_menu using page.getProperty(..) > and can show or hide the menu. > > Is there a way to do something like the above in Rails? > For example a template t1.rhtml could be: > <div> > Some content > <!-- can I set a variable show_menu here --> </div><% @show_menu = true -%> (note: the - in front of the %> prevents an empty line being written in the HTML)> And this is the layout1.rhtml (layout file that t1 gets decorated with): > <html> > <head></head> > <body> > <!-- can I use the variable show_menu here that might be defined in the > template this file is decorating --> <div id=''menu''></div><% if @show_menu -%> <div id=''menu''></div> <% end -%>> <div id=''main''> <%=@content_for_layout%> </div> </body> </html>regards Justin
Just found the answer to my post. If somebody else is looking for an answer to a similar question: In Rails, layouts have access to all the same data that is available to conventional templates. Any instance variables set in the normal template will be available in the layout. Following my previous example you could do this: My template file t1.rhtml: <div> Some content <% @show_menu = "true" %> </div> And this is the layout1.rhtml (layout file that t1 gets decorated with): <html> <head></head> <body> <%if @show_menu == "true"%> <div id=''menu''></div> <%end%> <div id=''main''> <%=@content_for_layout%> </div> </body> </html> -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Mufaddal Khumri Sent: Friday, March 31, 2006 2:46 PM To: rails@lists.rubyonrails.org Subject: [Rails] Layout Question .. (Sitemesh related) Hi, I understand that the layout concept is very similar to OpenSymphony''s Sitemesh project. I have done a lot of development with Sitemesh, but am fairly new to Rails. I am wondering if there is a way for a layout in rails to find out value of a variable from a page it is decorating? In sitemesh you would be able to do <meta name="show_menu" content="true"> on some page being decorated and then in the layout page, you can access the value of show_menu using page.getProperty(..) and can show or hide the menu. Is there a way to do something like the above in Rails? For example a template t1.rhtml could be: <div> Some content <!-- can I set a variable show_menu here --> </div> And this is the layout1.rhtml (layout file that t1 gets decorated with): <html> <head></head> <body> <!-- can I use the variable show_menu here that might be defined in the template this file is decorating --> <div id=''menu''></div> <div id=''main''> <%=@content_for_layout%> </div> </body> </html> Is this possible in rails? If yes, how? Thanks, Mufaddal. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails