2014-01-09 50 views
1

我试图用嵌套渲染Rails3中的谐音与HAML

#whitepanelMID 
    #groups_view_scroller 
    = render 'show' do 
     = render 'short_field', locals: {label: 'Name:', value: @group.name} 
     = render 'short_field', locals: {label: 'Description:', value: @group.description} 

以下

一个HAML模板,谐音是_show.html.haml(注意使用产量的)

%table#vert_table.no_borders{ cellpadding: '0', cellspacing: '0'} 
    %tbody 
    %tr 
     %td{ cols: 2 } My table 
    = yield 

和_short_field.html.haml

%tr 
    %th.vert_table_heads= label 
    %td= value 

问题是收益率似乎没有工作。

什么是正确的使用方法block in render in HAML?

更新

我找到了一个解决办法,我不喜欢。

在HAML模板捕获输出像

#whitepanelMID 
    #groups_view_scroller 
    - rows = capture_haml do 
     = render partial: 'short_field', locals: {field_label: 'Name:', value: @group.name} 
     = render partial: 'short_field', locals: {field_label: 'Site:', value: @group.site.description} 
    = render partial: 'show', locals:{ content: rows} 
    %br/ 

与修改的部分_show.html.haml与content变量,而不是yield

%table#vert_table.no_borders{ cellpadding: '0', cellspacing: '0'} 
    %tbody 
    %tr 
     %td{ cols: 2 } My table 
    != content 

高兴听到一个更好的办法!

+1

你可以试试看看这个http://stackoverflow.com/questions/2951105/rails-render-partial-with-block – vidaica

+0

谢谢@vidaica,我用'=渲染布局:..'并且做招! – carlosayam

回答

4

仅仅因为@vidaca提供的链接是ERB,我想发布一个等效的HAML。

layout:使用使用包装模板

#whitepanelMID 
    #groups_view_scroller 
    = render layout: 'show', locals:{ table_title: 'My table'} 
     = render partial: 'short_field', locals: {field_label: 'Name:', value: @group.name} 
     = render partial: 'short_field', locals: {field_label: 'Site:', value: @group.site.desccription }   
    %br/ 

和_show.html.haml部分(包装)时等

%table#vert_table.no_borders{ cellpadding: '0', cellspacing: '0'} 
    %tbody 
    %tr 
     %td{ cols: 2 }= table_title 
    = yield 

(在这种情况下short_field)包裹的泛音工作原样。

希望可以帮助别人。