Home  >  Article  >  WeChat Applet  >  WeChat Mini Program Tutorial Template

WeChat Mini Program Tutorial Template

黄舟
黄舟Original
2018-05-15 10:18:231936browse

Template

WXML provides a template (template), which can define code snippets in the template and then call them in different places.

Define template

Use the name attribute as the name of the template. Then define the code snippet in dcdc0fa59b5fea5bdae0d810c3919fcd, such as:

<!--  
 index: int  
 msg: string  
 time: string  
-->  
<template name="msgItem">  
 <view>  
 <text> {{index}}: {{msg}} </text>  
 <text> Time: {{time}} </text>  
 </view>  
</template>

Use the template

Use the is attribute to declare the template you need to use, and then pass in the data required by the template , such as:

09a523eb005576c6588ac061c054c651

Page({  
 data: {  
 item: {  
 index: 0,  
 msg: &#39;this is a template&#39;,  
 time: &#39;2016-09-15&#39;  
 }  
 }  
})

is attribute can use Mustache syntax to come at runtime Decide which template needs to be rendered:

<template name="odd">  
 <view> odd </view>  
</template>  
<template name="even">  
 <view> even </view>  
</template>  
  
<block wx:for="{{[1, 2, 3, 4, 5]}}">  
 <template is="{{item % 2 == 0 ? &#39;even&#39; : &#39;odd&#39;}}"/>  
</block>

The scope of the template

The template has its own scope and can only use the data passed in by data.

The above is the content of the WeChat applet tutorial template. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn