当前位置:网站首页>Mustache syntax
Mustache syntax
2022-07-01 01:14:00 【qiphon3650】
mustache grammar
mustache Templates , Is used to construct html The page content . In practice , When you want to call different functions in the same template to render the picture , On the premise that it has been customized , You can manually judge the parameters passed in when rendering the page .【 In case of bad judgment , We can judge by changing the incoming parameters 】 Let me see the syntax of the template with Xiaobai .
Mustache The template syntax of is very simple , Just a few :
{
{data}}
{
{#data}}{
{/data}}
{
{^data}}{
{/data}}
{
{.}}
{<partials}}
{
{
{data}}}
{
{!comments}}
With the help of Demo Let's give a brief introduction to grammar :
<script type="text/javascript" src="mustache.js"></script>
<script type="text/javascript"> var data = {
"name": " xiaohua ", "msg": {
"sex": " female ", "age": " 22 ", "hobit": " reading " }, "subject": ["Ch","En","Math","physics"] } var tpl = '<p> {
{name}}</p>'; var html = Mustache.render(tpl, data); alert ( html ); </script>
{ {data}}
Namely Mustache Identifier for , In curly brackets data Indicates the key name , The function of this sentence is to directly output the key value that matches the key name , for example :
var tpl = '{
{name}}';
var html = Mustache.render(tpl, data);
// Output :
xiaohua
{ {#data}} { {/data}}
With # Start 、 With / End means block , If msg If it's worth it ( loop ) Rendering , For example, rewrite Demo Medium tpl:
var tpl = '{
{#msg}} <p>{
{sex}},{
{age}},{
{hobit}}</p> {
{/msg}}';
var html = Mustache.render(tpl, data);
// Output :
<p> female, 22, reading</p>
Be careful : If { {#data}} { {/data}} Medium data The value is null, undefined, false; No rendering output .
{ {^data}} { {/data}}
This grammar is related to { {#data}} { {/data}} contrary , It's when data The value is null, undefined, false The content of the block will be rendered and outputted .
var tpl = {
{^nothing}} Did not find nothing The key name will render this {
{/nothing}};
var html = Mustache.render(tpl, data);
// Output :
Did not find nothing The key name will render this
{ {.}}
{ {.}} Represents enumeration , You can loop through the entire array , for example :
var tpl = '{
{#subject}} <p>{
{.}}</p> {
{/subject}}';
var html = Mustache.render(tpl, data);
// Output :
<p>Ch</p> <p>En</p> <p>Math</p> <p>physics</p>
{ {>partials}}
With > Start with the submodule , Such as { {> msg}}; When the structure is complex , We can use this syntax to split a complex structure into several small submodules , for example :
var tpl = "<h1>{
{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);
// Output :
<h1>xiaohua</h1>
<ul>
<li>female</li>
<li>22</li>
<li>reading</li>
</ul>
{ { {data}}}
{ {data}} The output will translate special characters such as , If you want to keep the content as it is, you can use { { {data}}}, for example :
var tpl = '{
{#msg}} <p>{
{
{age}}}</p> {
{/msg}}'
// Output :
<p>22</p>
{ { !comments}}
! Notation , Nothing will be rendered after annotation .
{
{ ! Here are the notes }}
// Output :
At work , If the content on the page is obtained from the background and rendered to the page , We can use mustache The template , It is worth noting that ,render The data of must match the key name .
边栏推荐
猜你喜欢

Analyze the maker education path integrating the essence of discipline

ORB-SLAM2源码学习(二)地图初始化

Analysis of blocktoken principle

C language file operation for conquering C language

Golang treasure house recommendation

Oracle临时表详解

Koa koa-combine-routers 分路由管理

Host FL Studio fruit music production daw20.9

P4 learning - p4runtime

K210工地安全帽
随机推荐
魔王冷饭||#101 魔王解惑数量多与质量;员工管理;高考志愿填报;游戏架构设计
ESP8266 RC522
用Steam教育启发学生多元化思维
解读创客教育所蕴含的科技素养
Vnctf 2022 cm CM1 re reproduction
Join table query select generation
女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!
Using asyncio for concurrency
5. TPM module initialization
文件服务设计
Koa koa-combine-routers 分路由管理
解析创客教育实践中的智慧原理
剑指 Offer 19. 正则表达式匹配
Some views on libco
機器人編程的培訓學科類原理
ORB-SLAM2源码学习(二)地图初始化
DLS-20型双位置继电器 220VDC
Two-stage RO: part 1
pytorch编程知识(2)
Integer to hexadecimal string PTA