Haml displays several fragments in the layout

How can I indent the code correctly?

app / views / layouts / shared.html.haml:

= render :partial => "shared/head" = yield = render :partial => "shared/footer" 

app / views / general / _head.html.haml:

 !!!XML !!!1.1 %html{"xml:lang" => "pl", :xmlns => "http://www.w3.org/1999/xhtml"} %head %title some title %body .container 

app / views / general / index.html.haml:

 %p Hello World! 

app / views / general / _footer.html.haml:

 .footer Some copyright text 

HTML output:

 <!DOCTYPE html> <html xml:lang='pl' xmlns='http://www.w3.org/1999/xhtml'> <head> <title> some title </title> </head> <body> <div class='container'></div> </body> </html> <p> Hello World! </p> <div id='footer'> Some copyright text </div> 
+6
html ruby-on-rails-3 partial partials haml
source share
2 answers

You should use app/views/layout for this and yield actual content:

Example

Update

app/views/layout/shared.html.haml :

 !!! 1.1 %html = render "shared/head" %body .container = yield = render "shared/foot" 
+5
source share

Looks like I'm pretty late for the party here, but maybe someone else will run into this and have to solve the same problem (as I did this evening).

In my case, I have a more complicated setup for opening an HTML tag and several different layouts, so I did not want a repetition. My opening HTML tag has conditions for different versions of IE and initially looked something like this:

 - # /app/views/layouts/shared/_head.html.haml !!! 5 <!--[if lt IE 7 ]> <html lang="en" class="no-js ie ie6"> <![endif]--> <!--[if IE 7 ]> <html lang="en" class="no-js ie ie7"> <![endif]--> <!--[if IE 8 ]> <html lang="en" class="no-js ie ie8"> <![endif]--> <!--[if IE 9 ]> <html lang="en" class="no-js ie ie9"> <![endif]--> <!--[if (gte IE 9)|!(IE)]><!--> %html{ 'xml:lang' => 'en', lang: 'en', class: 'no-js'} <!--<![endif]--> %head - # and so on... 

I had the same problem with </html> terminating prematurely, so I broke the HTML tag from the _head particle (leaving the main tag there) and created the following helper to solve the problem:

 # /app/helpers/application_helper.rb module ApplicationHelper def render_html_tag(&block) markup = capture_haml &block haml = Haml::Engine.new <<-HAML !!! 5 <!--[if lt IE 7 ]> <html lang="en" class="no-js ie ie6"> <![endif]--> <!--[if IE 7 ]> <html lang="en" class="no-js ie ie7"> <![endif]--> <!--[if IE 8 ]> <html lang="en" class="no-js ie ie8"> <![endif]--> <!--[if IE 9 ]> <html lang="en" class="no-js ie ie9"> <![endif]--> <!--[if (gte IE 9)|!(IE)]><!--> %html{ 'xml:lang' => 'en', lang: 'en', class: 'no-js'} <!--<![endif]--> = markup HAML obj = Object.new haml.def_method(obj, :render, :markup) obj.render(markup: markup) end end 

This is a bit dirty, and maybe you can clean it up a bit, but the main idea is to use the haml engine #def_method , which allows the layout to look something like this:

 - # /app/views/layout/application.html.haml = render_html_tag do = render 'layouts/shared/head' %body = yield = render 'layouts/shared/footer' 
+1
source share

All Articles