如何在 Google Closure Template 中传递 html 参数

use*_*063 2 closures google-closure-templates

伙计们,我想传递一个包含 html 字符的参数Google Closure Template,但我得到的只是文字 html 文本。这该怎么做?

到目前为止我尝试过的是:

{template .modal autoescape="strict" kind="html"}
   {$html_content}
{/template}
Run Code Online (Sandbox Code Playgroud)

我一直在读这个,但它不是很有帮助。谢谢

Nik*_*iko 5

{template .modal}
   {$html_content |noAutoescape}
{/template}
Run Code Online (Sandbox Code Playgroud)

将打印您的 HTML。但考虑到|noAutoescape不鼓励在模板中使用。

不鼓励:当内容安全的断言远离它的创建位置时,很容易意外引入 XSS 攻击。取而代之的是,将内容包装为经过消毒的内容,并在创建时轻松展示安全性。
谷歌关闭模板功能和打印指令


或者,如果您确定$html_contentHTML 是“安全的”,您可以在将参数传递给模板的地方指定它:

goog.require('soydata.VERY_UNSAFE');
goog.require('template.namespace');

var container = document.getElementById('modal');
var html = '<strong>HTML you trust!</strong>';

container.innerHTML = template.namespace.modal({
  html_content: soydata.VERY_UNSAFE.ordainSanitizedHtml(html);
});
Run Code Online (Sandbox Code Playgroud)

然后您的初始模板将按原样打印 HTML:

/**
 * @param html_content HTML markup
 */
{template .modal autoescape="strict" kind="html"}
   {$html_content}
{/template}
Run Code Online (Sandbox Code Playgroud)