简体   繁体   English

内部的 Mustache 模板字符串呈现为 HTML

[英]Mustache template string inside render as HTML

I am using Mustache to render templates.我正在使用 Mustache 来渲染模板。

I have this json object:我有这个 json 对象:

  {
    title: "Foo bar", 
    content: "<p> Html here </p>", 
    footer: "footer content here"
  }

I have a Mustache template like:我有一个 Mustache 模板,例如:

  <div id="box">
    <div id="title"> {{title}} </div> 
    <div id="content"> {{content}} </div> 
    <div id="footer"> {{footer}} </div> 
  </div>

My problem is that the html within the variable content is not getting rendered but instead is just getting printed to the screen.我的问题是变量内容中的 html 没有被渲染,而是被打印到屏幕上。

I see (in non-view source window): <p> Html here </p> , where I would only want to see that if I viewed the page source.我看到(在非查看源代码窗口中): <p> Html here </p> ,如果我查看页面源代码,我只想看到它。

How can I fix such that when I pass in a string to a mustache template the HTML inside gets rendered?我该如何解决,当我将字符串传递给 mustache 模板时,内部的 HTML 会被渲染? I am calling mustache.render(templates.all,data);我打电话给 mustache.render(templates.all,data); as my call to mustache.作为我对小胡子的呼唤。

From the Mustache documentation :来自Mustache 文档

All variables are HTML escaped by default.默认情况下,所有变量都是 HTML 转义的。 If you want to return unescaped HTML, use the triple mustache: {{{name}}}.如果要返回未转义的 HTML,请使用三重胡子:{{{name}}}。

So you just have to use eg.所以你只需要使用例如。 {{{content}}} in your template: {{{content}}}在您的模板中:

  <div id="box">
    <div id="title"> {{title}} </div> 
    <div id="content"> {{{content}}} </div> 
    <div id="footer"> {{footer}} </div> 
  </div>

I am using Mustache to render templates.我正在使用Moustache渲染模板。

I have this json object:我有这个json对象:

  {
    title: "Foo bar", 
    content: "<p> Html here </p>", 
    footer: "footer content here"
  }

I have a Mustache template like:我有一个Mustache模板,例如:

  <div id="box">
    <div id="title"> {{title}} </div> 
    <div id="content"> {{content}} </div> 
    <div id="footer"> {{footer}} </div> 
  </div>

My problem is that the html within the variable content is not getting rendered but instead is just getting printed to the screen.我的问题是可变内容内的html不会被渲染,而只是被打印到屏幕上。

I see (in non-view source window): <p> Html here </p> , where I would only want to see that if I viewed the page source.我看到(在非视图源窗口中): <p> Html here </p> ,在这里,我只想查看页面源代码就可以看到它。

How can I fix such that when I pass in a string to a mustache template the HTML inside gets rendered?如何解决这样的问题:当我将字符串传递到小胡子模板时,内部的HTML会呈现出来? I am calling mustache.render(templates.all,data);我正在打电话给mustache.render(templates.all,data); as my call to mustache.作为我呼唤小胡子的电话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM