当前位置:网站首页>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 .
边栏推荐
- 機器人編程的培訓學科類原理
- Cmu15445 (fall 2019) project 1 - buffer pool details
- 2022 is half way through. It's hard to make money
- (学习力+思考力) x 行动力,技术人成长的飞轮效应总结
- 什么是产品思维
- Share your own terminal DIY display banner
- Metauniverse and virtual reality (II)
- Practical shell knowledge
- Get to know the drawing component of flutter - custompaint
- DLS-20型双位置继电器 220VDC
猜你喜欢
Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)
Cmu15445 (fall 2019) project 1 - buffer pool details
C language file operation for conquering C language
Golang treasure house recommendation
New content violation degree determination scana bad information monitoring capability update issue 5
The longest selling mobile phone in China has been selling well since its launch, crushing iphone12
A letter to 5000 fans!
解析创客教育实践中的智慧原理
Vnctf 2022 cm CM1 re reproduction
Oracle temporary table explanation
随机推荐
Get screen height
蒹葭苍苍,白露为霜。
A proper job is a good job
How to scroll uitableview to a specific position - how to scroll uitableview to specific position
NE555 waveform generator handle tutorial NE555 internal structure (I)
实验八 T-sql,存储过程
C # generates PPK files in putty format (supports passphrase)
Sword finger offer 18 Delete the node of the linked list
问题解决:如何管理线程私有(thread_local)的指针变量
K210工地安全帽
【网络丢包,网络延迟?这款神器帮你搞定所有!】
Usage of C set
双链表:初始化 插入 删除 遍历
关于Unity一般的输入操作方式
Packing and unpacking of C #
Sword finger offer 19 Regular Expression Matching
Day31-t1380-2022-02-15-not answer by yourself
Error msb8031: building an MFC project for a non Unicode character set is deprecated
None of the following candidates is applicable because of a receiver type mismatch
Double linked list: initialize insert delete traversal