如何使用 mustache php 完整呈现带有大括号的纯正 mustache js 模板

How to use mustache php to render unadulterated mustache js template with curly braces intact

我想在小胡子 php 中呈现一个 javascript 小胡子模板。如何在 php 小胡子的脚本标签内渲染 {{foo}} 并转义 {{foo}} 大括号,以便稍后将其渲染为 mustache.js 模板?

我明白了。我将 php 小胡子模板中的所有双花括号 {{var}} 替换为尖括号 + 百分号 <%var%>。这样,分隔符在 php 小胡子渲染后仍然存在。然后使用 mustache.js 渲染,我在模板调用和渲染之间添加了几行以设置自定义分隔符和解析模板。

var template = $("#mytemplate").html();
var customTags = [ '<%', '%>' ];
Mustache.parse(template, customTags);
var output = Mustache.render(template, data);