【问题标题】:Mustache nested templates小胡子嵌套模板
【发布时间】:2012-04-18 11:42:03
【问题描述】:

我想知道这是否可能。在本例中,HTML 文件为:

{{i18n.sample_message}}

在我的渲染函数中我有这个:

var json = {
 i18n:i18n,
 sampleDate:'10/10/10'
}
$('div').html(Mustache.to_html(template,json);

i18n 文件是一个对象并且会有一个键:

sample_message:some long message
date is: {{json.sampleDate}}

现在我在屏幕上看到{{json.sampleDate}}。我尝试以分号结束字符串并使用 + 连接值,但这也不起作用。

暂时我没有将{{json.sampleDate}} 放在i18n 地图中,我将我的html 更改为

{{i18n.sample_message}}{{json.sampleDate}}

实际上,我有一些很长的段落需要注入一些动态值。

【问题讨论】:

    标签: javascript mustache


    【解决方案1】:

    我能够以一种丑陋的方式让它工作。如果有更清洁/更好的东西,请评论/编辑。

    我不得不调用 Mustache.to_html 两次。

    var html = Mustache.to_html(template,json);
    return Mustache.to_html(html,json);
    

    通过再次调用 to_html,Mustache 找到了 {{json.sampleDate}} 并将其替换为我的 json 中的值。

    【讨论】:

    • 经过一番思考,我放弃了 i18n 的把手,并以不同的方式排列了我的 HTML。风格上我认为它更干净。
    猜你喜欢
    • 2011-10-14
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多