版权声明:本做品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。转载请注明来源http://blog.youkuaiyun.com/azureternite https://blog.youkuaiyun.com/azureternite/article/details/53037695html
场景
使用{{expression}}时,输出的内容会被转义,如:express
context为.net
{
title: '
Hello world
'}
结果生成html:code
最终在页面上显示的就是字符串
Hello world
htm若是不想进行html的转义,有两种方法:blog
{{{SafeString}}}ip
在模板中将两个花括号替换成三个花括号,便可避免html字符串被转义字符串
传入的context为get
{
title: '
Hello world
',body: '
Hello world
'}
生成的结果:string
Hello world
页面上显示的就是
Hello world
Hello world
SafeString
Handlebars提供一个SafeString方法,使用这个方法返回的值即使在{{expression}}中也不会被转义
Handlebars模板:
{{sayhi}}
JavaScript:
Handlebars.registerHelper('sayhi', function(){
var str = '
Hello world
';return new Handlebars.SafeString(str);
});
参考