我想知道这是否可能。在此示例中,HTML 文件将是:
{{i18n.sample_message}}
在我的渲染函数中我有这个:
var json = {
i18n:i18n,
sampleDate:'10/10/10'
}
$('div').html(Mustache.to_html(template,json);
i18n
文件是一个对象并且有一个键:
示例消息:some long message<br/>
date is: {{json.sampleDate}}
现在我得到 {{json.sampleDate}}
屏幕上。我试过在分号处结束字符串并使用 + 连接值,但这也不起作用。
暂时我不放{{json.sampleDate}}
在i18n
map 我将 html 更改为
{{i18n.sample_message}}{{json.sampleDate}}
实际上,我有一些长段落需要注入(inject)一些动态值。
最佳答案
我能够以一种丑陋的方式让它工作。如果有更清洁/更好的内容,请发表评论/编辑。
我不得不调用 Mustache.to_html 两次。
var html = Mustache.to_html(template,json);
return Mustache.to_html(html,json);
通过再次调用 to_html,Mustache 找到了 {{json.sampleDate}} 并将其替换为我的 json 中的值。
关于javascript - mustache 嵌套模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10016650/