node.js和Handlebars:已编译的HTML已被转义 [英] node.js and Handlebars: HTML compiled is escaped
问题描述
这是模板 index.html
{{CONTENT}}
这是代码
var fs = require(fs);
var handlebars = require(handlebars);
var data = {
CONTENT:< b> Hello world!< / b>
};
var templateFile = fs.readFileSync('./ index.html','utf8');
var template = handlebars.compile(templateFile);
var html = template(data);
问题是标签< B>
转义为& lt; B& gt;
我该如何避免这种情况?
从 handlebarsjs.com :
把手HTML转义由{{表达式}}返回的值。如果您的
不希望Handlebars转义价值,请使用三重藏匿。
< div class =entry>
< h1> {{title}}< / h1>
< div class =body>
{{{body}}}
< / div>
< / div>
带上下文:
{
title:所有关于< p>标签,
body:< p>这是关于& lt; p& gt;标签< / p>中
$ / code $ / pre
结果如下:
< div class =entry>
< h1>所有关于& lt; p& gt;标记< / H1>
< div class =body>
< p>这是关于& lt; p& gt;标记< / p为H.
< / div>
< / div>
然而,从我的角度来看,它可能会挫败将模板分开的目的,而不是您的js文件。
如果您使用预编译,请使用noEscape选项:
$ p $ handlebars.precompile(content,{noEscape:true})
Im using handlebars in a node aplication, and I have trouble.
This is the template index.html
{{CONTENT}}
This is the code
var fs = require("fs");
var handlebars = require("handlebars");
var data = {
CONTENT: "<b>Hello world!</b>"
};
var templateFile = fs.readFileSync('./index.html', 'utf8');
var template = handlebars.compile( templateFile );
var html = template(data);
The problem is that the tags <B>
are escaped to <B>
How can I avoid this?
From handlebarsjs.com :
Handlebars HTML-escapes values returned by a {{expression}}. If you don't want Handlebars to escape a value, use the "triple-stash".
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{{body}}}
</div>
</div>
with this context:
{
title: "All about <p> Tags",
body: "<p>This is a post about <p> tags</p>"
}
results in:
<div class="entry">
<h1>All About <p> Tags</h1>
<div class="body">
<p>This is a post about <p> tags</p>
</div>
</div>
However from my point of view it may defeat the purpose of having a template separated than you're js file.
If you use precompile then use noEscape option:
handlebars.precompile(content, {noEscape: true})
这篇关于node.js和Handlebars:已编译的HTML已被转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!