Detailed description of WeChat mini-app Template and simple examples

Source: Internet
Author: User
This article mainly introduces the details of the mini-program Template and the relevant information of simple examples. if you need more information, refer to the mini-program Template.

Template

WXML provides a Template. you can define code snippets in the Template and use them in different places. Ensure that the format and data are the same.

1-define a template

Use' 'Tag defines the template, name the template as tempName, and assign the value to the attribute name. Inside the tag, define the template structure. As follows:

 
 
   
    
   
     {{index}}: {{msg}} 
     
   
     Time: {{time}} 
    
  
 

2-use templates

Use Tag, where the template needs to be used. If you want to use data in the js file, you need to add the data attribute. As follows:

  
 
 
 The same information is displayed three times on the page. The data in data comes from the js file, as follows:
 Page ({data: {item: {index: 0, msg: 'This is a template', time: '2017-09-15 '}}})

3-is attribute

The is attribute can not only point to the rendering template statically, but also use the Mustache syntax to dynamically decide which template to render. As follows:

 // Templates
   
  
   
Odd
  
 
   
  
   
Even
  
 // The is attribute uses the Mustache syntax to dynamically render the template
   
  
 

The above code displays the odd or even based on the conditions on the page.

4-Template reference

The preceding templates are defined and referenced in the same wxml file, and the template definitions and references can be separated. That is, defining templates in one file, while defining templates in one or more wxml files.
Reference the template from an external file and use the import src = "templateUrl"/> label. Similarly, the is attribute is used to point to the tag to be referenced.
The directory is as follows:

-pages  |--index    |--index.js    |--index.json    |--index.wxml    |--index.wxss  |--template    |--template.js    |--template.json    |--template.wxml    |--template.wxss

To use the template defined in template in index. wxml, you must first use import in index to import the template:

 
 
  

Pay attention to the import scope, which only references the template in the target file. For example, C import B, B import A. in C, you can use the template defined by B. in B, you can use the template defined by A, but C cannot use the template defined by.

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

For more details about the small program Template and simple examples, please follow the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.