我有一个HTML字符串,我希望在Mustache中呈现为HTML。但是,该字符串还包含一个Mustache条件。当我在Chrome Dev工具中检查呈现的HTML时,条件似乎是直接打印在input元素中,而不是被处理。我认为这可能是结束括号中的正斜杠的问题。在Chrome Dev工具中,/在渲染时被移除。因此,我尝试使用HTML十进制代码。这也不起作用。
这就是我呈现HTML字符串的地方:
<span>{{{response.additionalInfo}}}</span>
这是我传递给视图的对象。additionalInfo属性是一个嵌入了Mustache条件的属性:
var response = {
templateType: "optIn",
header: "Opt-In",
additionalInfo: "<label><input type='checkbox' id='notOptIn' name='isOptIn' {{^response.isOptIn}}checked{{/response.isOptIn}}> YES</label>",
isOptIn: false,
}
有没有可能在Mustache将呈现为HTML的字符串中添加一个Mustache条件?如果是这样,我如何转义结束标记中的/?
发布于 2018-06-26 02:56:27
我不能通过一个对Mustache的调用就让它工作。
无论如何,我设法通过在response.additionalInfo
中转换模板并将结果存储在response.additionalInfo
中来使其工作。
然后我转换了你提供的模板。
使用isOptIn = false
:
var response = {
templateType: "optIn",
header: "Opt-In",
additionalInfo: "<label><input type='checkbox' id='notOptIn' name='isOptIn' {{^response.isOptIn}}checked{{/response.isOptIn}}> YES</label>",
isOptIn: false,
}
var template = "<span>{{{response.additionalInfo}}}</span>";
document.getElementById("run").addEventListener("click", function() {
response.additionalInfo = Mustache.to_html(response.additionalInfo, window);
html = Mustache.to_html(template, window);
document.getElementById("container").innerHTML = html;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>
<script src="https://mustache.github.io/extras/mustache.js"></script>
<button id="run">Run</button>
<div id="container"/>
使用isOptIn = true
:
var response = {
templateType: "optIn",
header: "Opt-In",
additionalInfo: "<label><input type='checkbox' id='notOptIn' name='isOptIn' {{^response.isOptIn}}checked{{/response.isOptIn}}> YES</label>",
isOptIn: true,
}
var template = "<span>{{{response.additionalInfo}}}</span>";
document.getElementById("run").addEventListener("click", function() {
response.additionalInfo = Mustache.to_html(response.additionalInfo, window);
html = Mustache.to_html(template, window);
document.getElementById("container").innerHTML = html;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>
<script src="https://mustache.github.io/extras/mustache.js"></script>
<button id="run">Run</button>
<div id="container"/>
https://stackoverflow.com/questions/51029384
复制相似问题