Mustache

Mustache语法记录

Mustache的类型

  • {{data}}
  • {{#data}} {{/data}}
  • {{^data}} {{/data}}
  • {{.}}
  • {{>partials}}
  • {{{data}}}、{{&}}
  • {{!comments}}
  • {{= =}}

Mustache类型详解

  • {{data}}

    用于传入变量值

    input:
    data = “123”
    
    mustache temlate:
    {{data}}
    
    generator file content:
    123
    
  • {{#data}} {{/data}}: section【true、false、list】

    用于遍历传入值,类似数组for循环,为空则不展示该块内容;数组内部一般是键值对

    input:
    data = [{"name":"peter"},{"name":"tiny"}]
    
    mustache temlate:
    {{#data}}
    hellow {{name}}
    {{/data}}
    
    generator file content:
    hello peter
    hello tiny
    
  • {{^data}} {{/data}}

    与{{#data}} 相反,成对存在相当于if-else

    input:
    {  "persons": [] }
    
    mustache temlate:
    {{#persons}}
      <b>{{name}}</b>
    {{/persons}}
    {{^persons}}
      No person.
    {{/persons}}
    
    generator file content:
    No person.
    
  • {{.}}

    表示枚举,遍历某个变量,单纯的数组

    input:
    names: ['Bill', 'Tom', 'Alan']
    
    mustache temlate:
    {{#names}}{{.}}。{{/names}}
    
    generator file content:
    Bill。Tom。Alan。
    
  • {{<partials}}

    input:
    {
        "name": Iface
        "methods":[
            "name": "m1",
            "name": "m2",
            "name": "m3",
        ]
    }
    
    mustache temlate:
    
    1. interface.mustache:
    public interface {{name}} {
    {{#methods}}
      {{> method}}
    {{/methods}}
    }
    
    2. method.mustache:
    public void {{name}}();
    
    generator file content:
    public interface Iface {
        public void m1();
        public void m2();
        public void m3();
    }
    
  • {{{data}}}、{{&}}

    和{{data}}类似,都是传入变量用来显示,但是内部内容显示的是 已转码(unescape)的,直接显示对应符号,而不是符号对应的编码值

    input:
    {"name":"<b>world</b>"}
    
    mustache temlate:
    hello, {{name}}!
    hello, {{{name}}}!
    
    generator file content:
    hello, &lt;b&gt;world&lt;/b&gt!
    hello, <b>world</b>!
    
  • {{!comments}}

    注释、描述

    input:
    {"name":"<b>world</b>"}
    
    mustache temlate:
    Hello{{! comment }}.
    
    generator file content:
    Hello.
    
  • {{= =}}

    修改定界符,默认{{}}为定界符

    {{=<% %>=}} -> <% %>
    <%={{ }}=%> -> {{}}
    
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Getting Started Use the Current Stable Version (7.1) Buil...
    Leonzai阅读 1,978评论 0 3
  • Mustache 是一款经典的前端模板引擎,在前后端分离的技术架构下面,前端模板引擎是一种可以被考虑的技术选型,随...
    __ee47阅读 673评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,971评论 6 342
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,342评论 0 3