


Sharing code examples of Mustache syntax for WeChat mini program development
Mustache syntax in wxml in the WeChat applet cannot be ignored, which makes me, who has never done front-end iOS before, confused. . . I checked online. . . Record it.
What is Mustache?
Mustache is a logic-less (light logic) template parsing engine. It is produced to separate the user interface from business data (content). It can generate A document in a specific format, usually a standard HTML document. When you want to call different functions in the same template to render the screen, you can manually judge the parameters passed in when rendering the page, provided that they have been customized.
For example, the code in the wxml of the mini program:
// 这里的{{ }}就是Mustache的语法。 <text>{{userInfo.nickName}}</text>,
{{keyName}} {{{keyName}}} {{#keyName}} {{/keyName}} {{^keyName}} {{/keyName}} {{.}} {{!comments}} {{>partials}}
{{keyName}} Several situations
Simple variable replacement: {{name}}
var data = { "name": "weChat" }; Mustache.render("{{name}} is excellent.",data); 返回 weChat is excellent.
The variable contains the html code,
如:<br>、<tr>等而不想转义,可以在用{{&name}} var data = { "name" : "<br>weChat<br>" }; var output = Mustache.render("{{&name}} is excellent.", data); console.log(output); 返回:<br>weChat<br> is excellent. 去掉"&"的返回是转义为:<br>weChat<br> is excellent. 另外,你也可以用{{{ }}}代替{{&}}。
If it is an object, its properties can also be declared
var data = { "name" : { "first" : "Chen", "last" : "Jackson" }, "age" : 18 }; var output = Mustache.render( "name:{{name.first}} {{name.last}},age:{{age}}", data); console.log(output); 返回:name:Chen Jackson,age:18
{{#keyName}} {{/keyName}} starts with # and ends with / to indicate a block. It will render the block one or more times based on the key value in the current context. Its function is very powerful, and functions like if and foreach can also be added to it.
var data = { "stooges" : [ { "name" : "Moe" }, { "name" : "Larry" }, { "name" : "Curly" }; var output = Mustache.render("{{#stooges}}<b>{{name}}</b>{{/stooges}}", data); console.log(output); 返回:<b>Moe</b> <b>Larry</b> <b>Curly</b>
{{^keyName}} {{/keyName}} Exception output
This syntax is the same as {{#keyName}} {{/keyName} }Similar, the difference is that it only renders and outputs the content of the block when the keyName value is null, undefined, or false. For example:
var data = { "name" : "<br>weChat<br>" }; var tpl = ‘{{^nothing}}没找到 nothing 键名就会渲染这段{{/nothing}}’; var output = Mustache.render(tpl, data); 返回:没找到 nothing 键名就会渲染这段
{{.}} represents an enumeration, which is used to loop out the entire array, for example:
var data = { "product": ["Macbook ","iPhone ","iPod ","iPad "] } var tpl = '{{#product}} <p>{{.}}</p> {{/product}}'; var html = Mustache.render(tpl, data); 返回:<p>Macbook </p> <p>iPhone </p> <p>iPod </p> <p>iPad </p>
{{! }} represents the comment Start with > to indicate submodules. When the structure is complex, we can use this syntax to split the complex structure into several small submodules. {{data}} The output will translate special characters. If you want to keep the content as it is, you can use {{{}}}, small There are so many programs that you can use for the most part. If you find anything else, I will update it. . . {{!这里是注释}}
{{>partials}} var tpl = "<h1 id="namme">{{namme}}</h1> <ul>{{>msg}}</ul>"
var partials = {msg: "{{#msg}}<li>{{sex}}</li><li>{{age}}</li><li>{{hobit}}</li>{{/msg}
var html = Mustache.render(tpl, data, partials);
//输出:
<h1 id="xiaohua">xiaohua</h1>
{{{data}}} var tpl = '{{#msg}} <p>{{{age}}}</p> {{/msg}}'
//输出:
<p>22</p>
The above is the detailed content of Sharing code examples of Mustache syntax for WeChat mini program development. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment