【问题标题】:How to pass html parameter in Google Closure Template如何在 Google Closure Template 中传递 html 参数
【发布时间】:2014-09-25 13:04:15
【问题描述】:
伙计们,我想在Google Closure Template 中传递一个包含 html 字符的参数,但我得到的只是文字 html 文本。如何做到这一点?
到目前为止我尝试过的是:
{template .modal autoescape="strict" kind="html"}
{$html_content}
{/template}
我一直在阅读this,但这并不是很有帮助。谢谢
【问题讨论】:
标签:
closures
google-closure-templates
【解决方案1】:
{template .modal}
{$html_content |noAutoescape}
{/template}
将打印您的 HTML。但考虑到不鼓励在模板中使用 |noAutoescape。
气馁:断言时很容易意外引入 XSS 攻击
内容是安全的远离它的创建位置。反而,
将内容包装为经过消毒的内容,并在其创建位置易于处理
证明安全。
– Google Closure Templates Functions and Print Directives
或者,如果您确定 $html_content 是“安全”HTML,您可以在将参数传递给模板的地方指定它:
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);
});
然后您的初始模板将按原样打印 HTML:
/**
* @param html_content HTML markup
*/
{template .modal autoescape="strict" kind="html"}
{$html_content}
{/template}