首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何格式化用于Mustache HTML呈现的Mustache条件内部字符串?

如何格式化用于Mustache HTML呈现的Mustache条件内部字符串?
EN

Stack Overflow用户
提问于 2018-06-26 02:00:26
回答 1查看 572关注 0票数 2

我有一个HTML字符串,我希望在Mustache中呈现为HTML。但是,该字符串还包含一个Mustache条件。当我在Chrome Dev工具中检查呈现的HTML时,条件似乎是直接打印在input元素中,而不是被处理。我认为这可能是结束括号中的正斜杠的问题。在Chrome Dev工具中,/在渲染时被移除。因此,我尝试使用HTML十进制代码。这也不起作用。

这就是我呈现HTML字符串的地方:

代码语言:javascript
复制
<span>{{{response.additionalInfo}}}</span>

这是我传递给视图的对象。additionalInfo属性是一个嵌入了Mustache条件的属性:

代码语言:javascript
复制
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条件?如果是这样,我如何转义结束标记中的/?

EN

回答 1

Stack Overflow用户

发布于 2018-06-26 02:56:27

我不能通过一个对Mustache的调用就让它工作。

无论如何,我设法通过在response.additionalInfo中转换模板并将结果存储在response.additionalInfo中来使其工作。

然后我转换了你提供的模板。

使用isOptIn = false

代码语言:javascript
复制
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;
});
代码语言:javascript
复制
<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

代码语言:javascript
复制
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;
});
代码语言:javascript
复制
<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"/>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51029384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档