网站推广.NET

网站推广.NET

mustache语法

来源:互联网
mustache 模板,用于构造html页面内容。在实际工作中,当同一个模板中想要调用不同的函数来渲染画面,在已经自定义好了的前提下,可以在渲染页面时对传入的参数进行手动判断。【在不好判断的情况下,我们可以通过改变传入参数来进行判断】跟小白我来看看该模板的语法吧。 

mustache 的模板语法很简单,就那么几个:

    {{data}}    {{#data}} {{/data}}    {{^data}} {{/data}}    {{.}}    {{<partials}}    {{{data}}}    {{!comments}}

借助 Demo 来对语法做简单的介绍:

<span style="color: #008080"> 1</span> <span style="color: #000000">...</span><span style="color: #008080"> 2</span> <script type="text/javascript" src="mustache.js"></script><span style="color: #008080"> 3</span> <script type="text/javascript"><span style="color: #008080"> 4</span> <span style="color: #0000ff">var</span> data =<span style="color: #000000"> {</span><span style="color: #008080"> 5</span>     "name": " xiaohua "<span style="color: #000000">,</span><span style="color: #008080"> 6</span>     "msg"<span style="color: #000000">: {</span><span style="color: #008080"> 7</span>         "sex": " female ",<span style="color: #008080"> <br> 8</span>         "age": " 22 "<span style="color: #000000">,</span><span style="color: #008080"> <br> 9</span>         "hobit": " reading "<br><span style="color: #008080"> 10</span>     <span style="color: #000000">},</span><span style="color: #008080">11</span> <span style="color: #000000">    "subject": ["Ch","En","Math","physics"<span style="color: #000000">] </span></span><span style="color: #008080">12</span>   <span style="color: #000000">}</span>  <br><span style="color: #008080">13</span> <br><span style="color: #008080">14</span>   <span style="color: #0000ff">var tpl = '<p> {{name}}</p>'<span style="color: #000000">; </span></span><span style="color: #008080">15</span> <span style="color: #0000ff"><span style="color: #000000"><span style="color: #008080">  <span style="color: #0000ff">var html =<span style="color: #000000"> Mustache.render(tpl, data);<br></span></span></span></span></span><span style="color: #008080">16</span> <span style="color: #008080">17</span><span style="color: #000000">  alert ( html );</span><span style="color: #008080">18</span> </script><span style="color: #008080">19</span> <span style="color: #000000">...</span>

{{data}}

{{}}就是 Mustache 的标示符,花括号里的 data 表示键名,这句的作用是直接输出与键名匹配的键值,例如:

<span style="color: #008080">1</span> <span style="color: #0000ff">var</span> tpl = '{{name}}'<span style="color: #000000">;</span><span style="color: #008080">2</span> <span style="color: #0000ff">var</span> html =<span style="color: #000000"> Mustache.render(tpl, data);</span><span style="color: #008080">3</span> <span style="color: #008000">//</span><span style="color: #008000">输出:</span><span style="color: #008080">4</span> xiaohua

{{#data}} {{/data}}

以#开始、以/结束表示区块,它会根据当前上下文中的键值来对区块进行一次或多次渲染,例如改写下 Demo 中的 tpl:

<span style="color: #008080">1</span> <span style="color: #0000ff">var</span> tpl = '{{#msg}} <p>{{sex}},{{age}},{{hobit}}</p> {{/msg}}'<span style="color: #000000">;</span><span style="color: #008080">2</span> <span style="color: #0000ff">var</span> html =<span style="color: #000000"> Mustache.render(tpl, data);</span><span style="color: #008080">3</span> <span style="color: #008080">4</span> <span style="color: #008000">//</span><span style="color: #008000">输出:</span><span style="color: #008080">5</span> <p> female, 22, reading</p>

注意:如果{{#data}} {{/data}}中的 data 值为 null, undefined, false;则不渲染输出任何内容。

{{^data}} {{/data}}

该语法与{{#data}} {{/data}}类似,不同在于它是当 data值为 null, undefined, false 时才渲染输出该区块内容。

<span style="color: #008080">1</span> <span style="color: #0000ff">var</span> tpl = {{^nothing}}没找到 nothing 键名就会渲染这段{{/nothing}};<span style="color: #008080">2</span> <span style="color: #0000ff">var</span> html =<span style="color: #000000"> Mustache.render(tpl, data);</span><span style="color: #008080">3</span> <span style="color: #008000">//</span><span style="color: #008000">输出:</span><span style="color: #008080">4</span> 没找到 nothing 键名就会渲染这段

{{.}}

{{.}}表示枚举,可以循环输出整个数组,例如:

<span style="color: #008080">1</span> <span style="color: #0000ff">var</span> tpl = '{{#subject}} <p>{{.}}</p> {{/subject}}'<span style="color: #000000">;</span><span style="color: #008080">2</span> <span style="color: #0000ff">var</span> html =<span style="color: #000000"> Mustache.render(tpl, data);</span><span style="color: #008080">3</span> <span style="color: #008000">//</span><span style="color: #008000">输出:</span><span style="color: #008080">4</span> <p>Ch</p> <p>En</p> <p>Math</p> <p>physics</p>

{{>partials}}

以>开始表示子模块,如{{> msg}};当结构比较复杂时,我们可以使用该语法将复杂的结构拆分成几个小的子模块,例如:

<span style="color: #008080"> 1</span> <span style="color: #0000ff">var</span> tpl = "<h1>{{namme}}</h1> <ul>{{>msg}}</ul>"<span style="color: #008080"> 2</span> <span style="color: #0000ff">var</span> partials = {msg: "<span style="color: #000000">{{#msg}}<li>{{sex}}</li><li>{{age}}</li><li>{{hobit}}</li>{{/msg}</span><span style="color: #008080"> 3</span> <span style="color: #000000">var html = Mustache.render(tpl, data, partials);</span><span style="color: #008080"> 4</span> <span style="color: #000000">//输出:</span><span style="color: #008080"> 5</span> <span style="color: #000000"><h1>xiaohua</h1></span><span style="color: #008080"> 6</span> <span style="color: #000000"><ul></span><span style="color: #008080"> 7</span> <span style="color: #000000">   <li>female</li></span><span style="color: #008080"> 8</span> <span style="color: #000000">   <li>22</li></span><span style="color: #008080"> 9</span> <span style="color: #000000">   <li>reading</li></span><span style="color: #008080">10</span> </ul>

{{{data}}}

{{data}}输出会将等特殊字符转译,如果想保持内容原样输出可以使用{{{}}},例如:

<span style="color: #008080">1</span> <span style="color: #0000ff">var</span> tpl = '{{#msg}} <p>{{{age}}}</p> {{/msg}}'<span style="color: #008080">2</span> <span style="color: #008000">//</span><span style="color: #008000">输出:</span><span style="color: #008080">3</span> <p>22</p>

{{!comments}}

!表示注释,注释后不会渲染输出任何内容。

<span style="color: #008080">1</span> {{!<span style="color: #000000">这里是注释}}</span><span style="color: #008080">2</span> <span style="color: #008000">//</span><span style="color: #008000">输出:</span>

在工作中,如果页面上的内容是从后台获取数据并渲染到页面上时,我们就可以使用mustache模板了,值得注意的是,render的数据一定要与键名相符合。

<br><br><br><br>
mustache