1

因为在 haml 编译为 html 之后会评估车把标签,并且车把在 haml 中算作平面文本,所以您不能缩进逻辑

  {{#if misc}}
  %b Misc Products
  Total:
  {{misc_total}}
  {{#each misc}}
  {{price_charged}}
  {{notes}}
  {{/each}}
  {{/if}}

这很难阅读。我做了一个 rails helper 让这个看起来更好看*。

def handlebars_helper(helper, &block)
  raise ArgumentError, "Missing block" unless block_given?
  open = ActiveSupport::SafeBuffer.new("{{##{helper}}}") # helper opening
  open.safe_concat capture(&block)
  open.safe_concat("{{/#{helper.split.first}}}") # helper closing
  concat(open)
end

它可以让你编写看起来像这样的haml

  - handlebars_helper 'if misc' do
    %b Misc Products
    Total:
    {{misc_total}}
    - handlebars_helper 'each misc' do
      {{price_charged}}
      {{notes}}

以前有没有这样做过,或者我有更好的方法吗?

*这个助手看起来有点乱,我对积木不太满意,我该如何清理它?

4

1 回答 1

0

如果你想使用 HAML 生成把手代码,你可以使用hamlbars,它会为你提供一些帮助帮助缩进的助手。不幸的是,你仍然会被 if/else 周围的尴尬缩进所困。

我推荐Emblem.js作为替代方案……语法比 HAML 更接近 Slim,但不会有丑陋的帮助代码污染你的模板,只是为了保留缩进。免责声明:我写了会徽。

于 2013-02-13T22:08:15.310 回答