# Referencing

WXML provides two referencing methods import and include.

# import

import can use the template defined by the target file in the current file, for example:

A template named item is defined in item.wxml:

<!-- item.wxml -->
<template name="item">
  <text>{{text}}</text>
</template>

By referencing item.wxml in index.wxml, you can use the item template:

<import src="item.wxml"/>
<template is="item" data="{{text: 'forbar'}}"/>

# import Scope

The concept of “scope” is used for import references. This means that the reference will import the template defined in the target file, but will not import any import template in the target file.

For example: In the case C import B, B import A, the template defined by B can be used in C and the template defined by A can be used in B. However, C cannot use the template defined by A.

<!-- A.wxml -->
<template name="A">
  <text> A template </text>
</template>
<!-- B.wxml -->
<import src="a.wxml"/>
<template name="B">
  <text> B template </text>
</template>
<!-- C.wxml -->
<import src="b.wxml"/>
<template is="A"/>  <!-- Error! Cannot use template when not import A. -->
<template is="B"/>

# include

include can introduce the entire code excluding <template/> <wxs/>. This is equivalent to copying the code to the location of include. For example:

<!-- index.wxml -->
<include src="header.wxml"/>
<view> body </view>
<include src="footer.wxml"/>
<!-- header.wxml -->
<view> header </view>
<!-- footer.wxml -->
<view> footer </view>