呈现“< %% =”与HAML [英] Rendering "<%%=" with HAML
问题描述
我有一个Backbone.js应用程序托管在Sinatra中,并通过ERB脚本呈现。骨干模板使用下划线模板功能,因此变量在ERB中呈现为:
I have a Backbone.js application hosted in Sinatra and rendered via an ERB script. The backbone templates are using the underscore template functionality so the variables are rendered in ERB like so:
<div id="<%%= variable %>">
额外的%转义该变量的呈现,并使用单个%呈现
The extra "%" escapes the rendering of that variable and renders it with a single "%" which is what the underscore template library would pick up.
我在升级到HAML时尝试了以下操作:
I tried the following while upgrading to HAML:
#"<%= id %>"
这不起作用。如何使用HAML完成相同的任务?
Which did not work. How do I accomplish the same task with HAML?
推荐答案
首先,您不能使用#
快捷方式以创建具有这样的值的 id
,您将需要做很长的路要走:
First, you can’t use the #
shortcut to create an id
with a value like that, you’ll have to do it the long way:
%div{:id => "<%= id %>"}
默认情况下,Haml将转义属性,因此会产生类似这样的结果:
By default, Haml will escape the attributes, so this will produce something like:
<div id='<%= id %>'></div>
这可能不是您想要的。您可以通过设置 :escape_attrs <来转义属性/ code>选项
设置为false。然后将产生所需的输出:
which is probably not what you want. You can turn of escaping of attributes by setting the :escape_attrs
option to false. This will then produce the desired output:
<div id='<%= id %>'></div>
请注意,此选项影响文档中的 all 属性。
Note that this option effects all attributes in the document.
另一种方法是在模板中使用一组不同的定界符一个>。例如,您可以将 {{...}}
与此配合使用:
An alternative would be to use a different set of delimiters in your templates. For example you could use {{...}}
with this:
_.templateSettings = {
interpolate : /\{\{(.+?)\}\}/g
};
现在Haml不会转义属性值。
Now Haml won’t escape the attribute values.
这篇关于呈现“< %% =”与HAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!