mustache.js使用笔记

Mustache 是一款经典的前端模板引擎,在前后端分离的技术架构下面,前端模板引擎
是一种可以被考虑的技术选型,随着重型框架(AngularJS、ReactJS、Vue)的流行,
前端的模板技术已经成为了某种形式上的标配,Mustache 的价值在于其稳定和经典:
主页: https://github.com/janl/mustache.js/
文档: https://mustache.github.io/mustache.5.html

Mustache 在使用的时候,会在页面上出现 {{person}} 这样的标签,载入的时候回显
示出来,然后立即被替换掉,这个对于页面的呈现是不够友好的,这是我在使用的过

程中遇到的一个痛点。

<!DOCTYPE html> 
<html> 
<head> 
<title>mustache.js</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head> 
<body>
<div id="demo"></div>
<script type="text/x-mustache-template" id="template">
    <div>
    Hello {{name}}
    You have just won {{value}} dollars!
    {{#in_ca}}
    Well, {{taxed_value}} dollars, after taxes.
    {{/in_ca}}
    </div> 
    <div>{{company}}</div>
    <div>{{{company}}}</div>
    <div>
        {{#repo}}
          <b>{{name}}</b>
        {{/repo}}
    </div>
    <div>
        {{#wrapped}}
          {{name}} is awesome.
        {{/wrapped}}
    </div>
    <div>
        {{#person?}}
            Hi {{name}}!
        {{/person?}}
    </div>   
    <div>
        {{#repo_new}}
          <b>{{name}}</b>
        {{/repo_new}}
        {{^repo_new}}
          No repos :(
        {{/repo_new}}
    </div>  
    <div>
        <h1>Today{{! ignore me }}.</h1>
    </div>
</script>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script src="https://cdn.bootcss.com/mustache.js/2.3.0/mustache.js"></script>
<script type="text/javascript">
    var template = $('#template').html();
    //Mustache.parse(template);
    var temp = Mustache.render(template, {
        "name": "徐同保",
        "value": 10000,
        "taxed_value": 10000 - (10000 * 0.4),
        "in_ca": true,
        "company": "<b>GitHub</b>",
        "repo": [
            { "name": "resque" },
            { "name": "hub" },
            { "name": "rip" }
        ],
        "wrapped": function() {
            return function(text, render) {
                return '<b style="color:green;font-size:36px;">' + render(text) + '</b>';
            }
        },
        "person?": { "name": "Jon" },
        "repo_new": []
    });
    $('#demo').html(temp);
</script>
</body> 
</html> 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐同保

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值