在Node.js的Swig模板中使用什么自动转义?

我正在尝试编写一个构build在Express上的行程应用程序。 Swig是模板引擎。 我对Swig的autoescapefunction感到困惑。 它究竟做了什么?

Swig 文档示例:

"Control auto-escaping of variable output from within your templates." // myvar = '<foo>'; {% autoescape true %}{{ myvar }}{% endautoescape %} // => <foo> {% autoescape false %}{{ myvar }}{% endautoescape %} // => <foo> 

我的代码:

 <script> {% autoescape false %} var all_hotels = {{ hotels | json }}; var all_restaurants = {{ restaurants | json }}; var all_things_to_do = {{ things_to_do | json }}; {% endautoescape %} </script> 

谢谢。

文档应该像这样读取:

 "Control auto-escaping of variable output from within your templates." // myvar = '<foo>'; {% autoescape true %}{{ myvar }}{% endautoescape %} // => &lt;foo&gt; {% autoescape false %}{{ myvar }}{% endautoescape %} // => <foo> 

所以当autoescape为true ,它会将HTML转义的variables内容。 我认为这是Swig的默认设置。

既然你想呈现JSONvariables,你的代码应该可以正常工作(closures自动转义以防止HTML转义的JSON内容)。 或者,您可以使用safe筛选器:

 var all_hotels = {{ hotels | safe | json }}; var all_restaurants = {{ restaurants | safe | json }}; ...