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, <b>world</b>! hello, <b>world</b>!
-
{{!comments}}
注释、描述
input: {"name":"<b>world</b>"} mustache temlate: Hello{{! comment }}. generator file content: Hello.
-
{{= =}}
修改定界符,默认{{}}为定界符
{{=<% %>=}} -> <% %> <%={{ }}=%> -> {{}}