0

我正在尝试这样做并使用 Middleman App 构建

%script{:type => "text/html", :id => "showItem"}
  {{#items}}
  %li
    %a{href: "#{{id}}"} {{showName}}
  {{/items}}

问题是这条线在另一条线内%a{href: "#{{id}}"}{{id}}{}

这是错误

SyntaxError at /show.html
/show.haml:110: syntax error, unexpected '}', expecting tASSOC ...tributes({}, nil, href: "#{{id}}")}>{{showName}}</a>\n ... ... ^ /show.haml:127: syntax error, unexpected ',', expecting '}' ...script>\n </body>\n</html>\n", -2, false); ... ^ /show.haml:131: syntax error, unexpected keyword_end, expecting '}'

Ruby    C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.3.3/lib/tilt/template.rb: in instance_eval, line 209
Web GET localhost/show.html

有没有办法解决这个问题?因为我需要使用 Handlebars 来解析标签属性中的变量。

谢谢。

4

1 回答 1

1

Haml将#{{id}}其评估为字符串插值。因此,它将外部#{...}视为要评估的 Ruby 代码的容器(就像任何标准 Ruby 代码一样),因此它尝试{id}在 Ruby 中进行评估。

Ruby 看到花括号id并期望有一个散列,这就是为什么你会得到“unexpected '}'”错误。

解决方案是转义#以避免字符串插值:

%a{href: "\#{{id}}"} {{showName}}
于 2013-10-02T14:26:23.713 回答