引用

SWAN 可以通过importinclude来引用文件。

import

通过importtemplate配合使用,可以将代码分离以及复用。

  1. <!-- personCard.swan-->
    <template name="person-card">
    <view>
    <text>位置: {{pos}}</text>
    <text>姓名: {{name}}</text>
    </view>
    </template>

personCard.swan里定义了一个模板,在index.swan里引用文件,并使用它的模板。

  1. <!-- index.swan-->
    <import src="./person-card.swan" />
    <template is="person-card" data="{{person}}" />

include

通过include可以将目标模板,整个(除了 template)引入到当前的位置,相当于inline

  1. <!-- detail.swan-->
    <include src="header.swan" />
    <view class="detail">body</view>

  1. <!-- header.swan-->
    <view class="header">header</view>

include 可以将目标文件除了 <template/>外的整个代码引入,相当于是拷贝到 include 位置,如:

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

  1. <!-- header.swan -->
    <view> header </view>

  1. <!-- footer.swan -->
    <view> footer </view>

事件处理Filter过滤器