2015-12-18 45 views
0

我正在撰写电子邮件视图,这在使用嵌套表格时特别讨厌。我的每一个电子邮件的许多部分的希望围绕它同样讨厌的克鲁夫特:如何烘干重复嵌套的HAML?

%table.centered 
    %tbody 
    %tr 
     %td 
     %table.one-col{"emb-background-style" => ""} 
      %tbody 
      %tr 
       %td.column 
       %div 
        .column-top   
       %table.contents 
        %tbody 
        %tr 
         %td.padded 
         COPY COPY COPY ETC. 

每个部分的内容是很多的副本和链接和诸如此类的东西,并把该进红宝石字符串或从单独的文件中渲染它会难以遵循。这是我想隐瞒的东西,而不是节的内容。

那么,有没有办法以某种方式连接cruft使HAML更少缩进和crufty?

+0

简短的回答是否定的,可能使其他文件是要走的路,你总是可以使用文件夹来整理它们,使其更容易跟随。所以所有的电子邮件模板都在'email /'里面。 – Leito

回答

5

这是可能的,无需呈现部分。

你可以像这样做一个辅助方法来隐藏所有'cruft',就像你放置它一样。

# app/helpers/application_helper.rb 
def nested_blk_call(&blk) 
    content_tag :div, class: "nested-tag-level-1" do 
    content_tag :div, class: "nested-tag-level-2" do 
     content_tag :div, class: "nested-tag-level-3" do 
     blk.call 
     "" 
     end 
    end 
    end 
end 

# some_view.html.haml 
= nested_blk_call do 
    .you-can-add-more-haml 
    COPY COPY COPY ETC. 

这会在浏览器输出

<div class="nested-tag-level-1"> 
    <div class="nested-tag-level-2"> 
    <div class="nested-tag-level-3"> 
     <div class="you-can-add-more-haml"> 
     COPY COPY COPY ETC. 
     </div> 
    </div> 
    </div> 
</div> 
+0

太棒了。谢谢 - 这个建议非常有帮助。 – Jason