Read Mustache's GitHub, learn the grammar, make a note
1. Simple variable exchange: {{name}}
1 var data = {"name": "Willy"};
2 Mustache.render ("{{name}} is awesome.", data);
Return results 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}}
1 var data = {
2 "name": "<br>Willy<br>"
3 };
4 var output = Mustache.render ("{{&name}} is awesome.", data);
5 console.log (output);
Results:<br>willy<br> is awesome.
The result of removing "&" is escaping to:<br> Willy<br> is awesome. ("<" and ">" are escaped by default)
3. If you are an object, you can declare its properties
1 var data = {
2 "name": {
3 "First": "Chen",
4 "Last": "Jackson"
5 },
6 "Age": 18
7 };
8 var output = Mustache.render (
9 "Name:{{name.first}} {{name.last}},age:{{age}}", data);
Ten Console.log (output);
Results: Name:chen jackson,age:18
4.{{#param}} This tag is very powerful and has the function of if assertion and foreach.
1 var data = {
2 "Nothin": True
3 };
4 var output = Mustache.render (
5 "shown.{ {#nothin}} Never shown! {{/nothin}} ", data);
6 console.log (output);
If nothin is empty or null, or FALSE to output shown. The opposite is Shown.never shown!.
5. Iteration
1 var data = {
2 "stooges": [{
3 "name": "Moe"
4 }, {
5 "name": "Larry"
6 }, {
7 "name": "Curly"
8 }]
9 };
var output = Mustache.render ("{{#stooges}}<b>{{name}}</b>{{/stooges}}",
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.
1 var data = {
2 "musketeers": ["Athos", "Aramis", "Porthos", "D" "Artagnan"]
3 };
4 var output = Mustache.render ("{{#musketeers}}* {{&.}} {{/musketeers}} ',
5 data);
6 console.log (output);
Output: * Athos
* Aramis
* Porthos
* D "" Artagnan
7. Iterative output can also be the result of a function return, function can read the current variable high and low text to get other properties to perform other manipulations
1 var data = {
2 "Beatles": [{
3 "FirstName": "John",
4 "LastName": "Lennon"
5}, {
6 "FirstName": "Paul",
7 "LastName": "McCartney"
8}, {
9 "FirstName": "George",
"LastName": "Harrison"
11}, {
"FirstName": "Ringo",
"LastName": "Starr"
14}],
"Name": function () {
return this.firstname + "" + this.lastname;
17}
18};
var output = Mustache
Render ("{{#beatles}} *{{name}}{{/beatles}}", data);
Console.log (output);
Output: *john Lennon
*paul McCartney
*george Harrison
*ringo Starr
8: The method can re-perform the expression in the variable
1 var data = {
2 "name": "{{age}}" + "Tater",
3 "bold": function () {
4 return function (text, render) {
5 Console.log (text);
6 return "<b>" + render (Text) + "</b>";
7 };
8 },
9 "Age": 18
Ten };
One -to-one var output = Mustache.render ("{{#bold}}hi {{name}}.{ {/bold}} ", data);
console.log (output);
Output results:
Hi {{age}}tater.
< B>hi 18tater.</b>
9.{{^}} is the opposite of {{#}} if the variable is null, undefined、
false、和空数组讲输出成果
10.{{! }} Stare
Original Address http://www.mysjtu.com/page/M0/S733/733819.html