我在 Node 应用程序中使用 Handlebars ,我遇到了麻烦。
这是模板 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>
转义到 <B>
我怎样才能避免这种情况?
最佳答案
来自 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>
在这种情况下:
{
title: "All about <p> Tags",
body: "<p>This is a post about <p> tags</p>"
}
结果:
<div class="entry">
<h1>All About <p> Tags</h1>
<div class="body">
<p>This is a post about <p> tags</p>
</div>
</div>
但是从我的角度来看,它可能会破坏将模板分开而不是 js 文件的目的。
如果您使用 precompile然后使用 noEscape 选项:
handlebars.precompile(content, {noEscape: true})
关于node.js 和 Handlebars : HTML compiled is escaped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15807799/