Haml render multiple partials in layout
How can I make the code indent correctly?
app/views/layouts/shared.html.haml:
= render :partial => "shared/head"
= yield
= render :partial => "shared/footer"
app/views/shared/_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/shared/index.html.haml:
%p
Hello World!
app/views/shared/_footer.html.haml:
.footer
Some copyright text
Rendered HTML output:
<!DOCTYPE html>
<html xml:lang='pl' xmlns='http://www.w3.org/1999/xh开发者_开发技巧tml'>
<head>
<title>
some title
</title>
</head>
<body>
<div class='container'></div>
</body>
</html>
<p>
Hello World!
</p>
<div id='footer'>
Some copyright text
</div>
You should use app/views/layout
for that and yield
the actual content:
Example
Update
app/views/layout/shared.html.haml
:
!!! 1.1
%html
= render "shared/head"
%body
.container
= yield
= render "shared/foot"
Looks like I'm pretty late to the party here, but perhaps someone else will encounter this and need to deal with the same problem (as I did this evening).
In my case, I have a more complex setup for the opening HTML tag, and several different layouts, so I didn't want all the repetition. My opening HTML tag has conditions for different IE versions and originally 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 was having the same problem with </html>
terminating prematurely, so I ripped the HTML tag out of the _head partial (leaving the head tag there) and created the following helper to deal with 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
It's a little messy and perhaps it can be cleaned up a bit, but the main idea is to take advantage of the haml engine's #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'
精彩评论