2016-08-07 27 views
1

我使用jsrender lib作为客户端模板生成,但整个网站的输出由thymeleaf(spring mvc)处理。问题是,JS模板包含condtiion即在客户端模板中使用&符号并使用thymeleaf进行渲染

<script id="main-menu-form-tmpl" type="text/x-jsrender"> 
     {{if (index && (index == 0 || ... 

但thymeleaf要求所有的&符号转义为&amp;,但这会导致jsrender失败。另一个解决方案发现here,但由于语法未知,jsrender也会失败。

有没有任何方法输出脚本的内容,而不用百里香叶引擎解析它?

回答

1

JsRender允许您从字符串以及脚本块中注册模板。

请参阅http://www.jsviews.com/#compiletmpl

因此,而不是写:

var mainTemplate = $.templates("#main-menu-form-tmpl"); 

,然后调用mainTemplate.render(...)mainTemplate.link(...)等,你可以删除,而不是你的模板脚本块,而是通过你的模板标记为一个字符串$.templates()为:

var mainTemplate = $.templates("... {{if (index && (index == 0 || ..."); 
... 

或者如果你想要的话,你可以保留脚本块声明,但是带有转义的&符号,然后将脚本块的内容作为字符串获取,并忽略&字符串,并将该字符串传递给你的模板TE定义:

var mainTemplateString = $("#main-menu-form-tmpl").text().replace(/&amp;/g, "&"); 
var mainTemplate = $.templates(mainTemplateString); 
... 

或者你可以用你的模板块<![CDATA,并再次,剥去包装纸,让你想传递给模板定义真正的模板标记字符串:

var mainTemplateString = $("#main-menu-form-tmpl").text().slice(16, -10); 
var mainTemplate = $.templates(mainTemplateString); 
... 
相关问题