Mustache.js Instructions for use

Source: Internet
Author: User

Read Mustache's GitHub, learn the grammar, make a note

1. Simple variable substitution: {{name}}

var data = {"name": "Willy"}; Mustache.render ("{{name}} is awesome.", data);

Returns the result Willy is awesome.


2. If the variable contains HTML code, such as:<br>, <tr> and so on, but do not want to escape can be used in {{&name}}

var data = {"name": "<br>Willy<br>"};        var output = Mustache.render ("{{&name}} is awesome.", data); Console.log (output);

Results:<br>willy<br> is awesome.

The result of removing "&" is escaping to:&lt;br&gt; Willy&lt;br&gt; is awesome. ("<" and ">" are escaped by default)


3. If you are an object, you can also interpret its properties

 var data = {               "Name"  : {                   "First"  :  "Chen",                   "Last"  :  "Jackson"               },               "Age"  : 18         };          var output = mustache.render (                   "Name:{{name.first}} {{name.last} },age:{{age}} ",  data);          console.log (output);

Results: Name:chen jackson,age:18


4.{{#param}} This tag is powerful and has the function of if judgment and foreach.

var data = {"Nothin": true}; var output = Mustache.render ("shown.{ {#nothin}} Never shown!            {{/nothin}} ", data); Console.log (output);

If nothin is empty or null, or FALSE, the shown is output. The opposite is Shown.never shown!.


5. Iteration

 var data = {               "Stooges"  : [ {                   "name"  :  "Moe"               }, {                   "name"  :  "Larry"               }, {                   "name"  :  "Curly"               } ]         };          var output = mustache.render ("{{#stooges}}<b>{{name}} </b>{{/stooges}} ", &NBSP;&NBSp;               data);          console.log (output);

Output:<b>moe</b>

<b>Larry</b>

<b>Curly</b>


6. If the iteration is an array, you can also use {{.}}. To replace each element

var data = {"musketeers": ["Athos", "Aramis", "Porthos", "D ' Artagnan"]}; var output = Mustache.render ("{{#musketeers}}* {{&.}}        {{/musketeers}} ", data); Console.log (output);

Output: * Athos

* Aramis

* Porthos

* D ' Artagnan


7. The output of the iteration can also be the result of a function return, function can read the context of the current variable to get other properties to perform other operations

 var data = {               "Beatles"  : [ {                   "FirstName"  :  "John",                   "LastName"  :  "Lennon"               }, {                   "FirstName"  :  "Paul",                   "LastName"  :  " McCartney "             }, {                   "FirstName" &NBSP;:   "George", &NBSP;&NBSP;&NBsp;               "LastName"  :  "Harrison"              }, {                   "FirstName" &NBSP;:   "Ringo",                   "LastName"  :  "Starr"               } ],              "Name" &NBSP;:  function ()  {                  return this.firstName +  " "  + this.lastName;              }          };         var output = mustache                  .render ("{{#beatles}} *{{name}}{{ /beatles}} ",  data);          console.log (output);

Output: *john Lennon

*paul McCartney

*george Harrison

*ringo Starr


8: The expression in the variable can be executed in the method

 var data = {               "Name"  :  "{{age}}"  +  "Tater",               "Bold"  : function ()  {                  return function (Text, render)  {                       console.log (text);                      return  "<b>"  + render (text)  +  " </b> ";                  };             },               "Age"  : 18         };          var output = mustache.render ("{{#bold}}hi  {{name}}. {{/bold}} ",  data);          console.log (output);

Output Result:

Hi {{Age}}tater.

<b>hi 18tater.</b>


9.{{^}} In contrast to {{#}}, if the variable is null, undefined, false, and empty array speak output


10.{{! }} Comment


Mustache.js Instructions for use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.