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】
用于遍歷傳入值,類似數(shù)組for循環(huán),為空則不展示該塊內(nèi)容;數(shù)組內(nèi)部一般是鍵值對
input: data = [{"name":"peter"},{"name":"tiny"}] mustache temlate: {{#data}} hellow {{name}} {{/data}} generator file content: hello peter hello tiny -
{{^data}} {{/data}}
與{{#data}} 相反,成對存在相當(dāng)于if-else
input: { "persons": [] } mustache temlate: {{#persons}} <b>{{name}}</b> {{/persons}} {{^persons}} No person. {{/persons}} generator file content: No person. -
{{.}}
表示枚舉,遍歷某個變量,單純的數(shù)組
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}}類似,都是傳入變量用來顯示,但是內(nèi)部內(nèi)容顯示的是 已轉(zhuǎn)碼(unescape)的,直接顯示對應(yīng)符號,而不是符號對應(yīng)的編碼值
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. -
{{= =}}
修改定界符,默認{{}}為定界符
{{=<% %>=}} -> <% %> <%={{ }}=%> -> {{}}