将模板中的内容嵌入HAML标记中的部分内容

时间:2017-07-20 09:02:20

标签: ruby haml indentation partials middleman

问题

我有一个部分,其中包含重复FAQ条目元素的HTML结构。

partials/_faqitem.haml

.question
    %a.faq-toggle{"data-toggle" => "collapse", :href => "\##{item}"} 
        = data.faq[item].q
    %div{:class => "collapse", :id => "#{item}"}
        Text directly in partial

我在faq.html.md.erb.haml模板中使用它:

= partial(:"partials/faqitem", :locals => { :item => "so" })

这会正确呈现HTML并从data/faq.yaml

插入数据
so:
  q: What is Stack Overflow?
  info: Some other info

当我尝试在= partial调用下在我的模板中添加更多文本时出现问题。我无法使用允许我在 div标签内的模板中呈现文本的正确缩进,就像渲染“直接部分文本”一样。

在模板中嵌套的示例:

= partial(:"partials/faqitem", :locals => { :item => "so" })
    Text in the template

= partial(:"partials/faqitem", :locals => { :item => "so" })
        Text in the template

根据嵌套级别,我会遇到以下错误之一:

syntax error, unexpected keyword_end, expecting end-of-input y in partial\n", 0, false);end;_hamlout.buffer << _hamlout.f ^

The line was indented 2 levels deeper than the previous line.

是否有任何方法可以直接在模板中添加文本,其呈现方式与嵌套在部分标记内的方式相同?

具体示例

为了更好地说明所需的结果,这是一个例子。

faq.html.md.erb.haml模板:

= partial(:"partials/faqitem", :locals => { :item => "so" })
    This is some text in the template.
    :markdown
        Now a *little* markdown to complicate things and then I will insert some information from data file, because I don't want to repeat myself.
    = data.faq.so.info

这与partials/_faqitem.haml

一起使用
.question
    %a.faq-toggle{"data-toggle" => "collapse", :href => "\##{item}"} 
        = spanmarkdown(data.faq[item].q)
    %div{:class => "collapse", :id => "#{item}"

应该产生与模板中的内容直接放在partial中相同的结果:

.question
    %a.faq-toggle{"data-toggle" => "collapse", :href => "\##{item}"} 
        = spanmarkdown(data.faq[item].q)
    %div{:class => "collapse", :id => "#{item}"}
        This is some text in the template.
        :markdown
            Now a *little* markdown to complicate things and then I will insert some information from data file, because I don't want to repeat myself.
        = data.faq.so.info

1 个答案:

答案 0 :(得分:0)

我不确定这种语法,我总是使用render来渲染我的局部和缩进效果很好。为什么不这样做:

= render "partials/faqitem", item: "so"
%div
  Text in the template