node.js和Handlebars:已编译的HTML已被转义 [英] node.js and Handlebars: HTML compiled is escaped

查看:228
本文介绍了node.js和Handlebars:已编译的HTML已被转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是模板 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 &lt;B&gt;

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 &lt;p&gt; tags</p>"
}

results in:

<div class="entry">
  <h1>All About &lt;p&gt; Tags</h1>
  <div class="body">
    <p>This is a post about &lt;p&gt; 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆