# template

WXML provides templates in which code snippets can be defined and then called in different places.

# Define the template

Use the name attribute as the name of the template. Then within 'define snippets of code, such as:

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

# Using templates

Using the is attribute, declare the template you need to use, and then pass in the data you need for the template, such as:

<template is="msgItem" data="{{...item}}"/>
Page({
  data: {
    item: {
      index: 0,
      msg: 'this is a template',
      time: '2016-09-15'
    }
  }
})

The is attribute can use the Mustache syntax to dynamically determine which template to render:

<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 ? 'even' : 'odd'}}"/>
</block>

# The scope of the template

Templates have their own scope and can only use data passed in by data and modules defined in the template definition file.