抖音开放平台Logo
开发者文档
“/”唤起搜索
控制台
  • 开发教程与代码示例
  • 入门
  • 小程序框架
  • 框架概述
  • 逻辑层
  • 视图层
  • TTML
  • TTML-数据绑定
  • TTML-列表渲染
  • TTML-条件渲染
  • TTML-模板
  • TTML-事件
  • TTML-引用
  • TTSS
  • 基础组件
  • Lynx 渲染引擎
  • 小程序运行时
  • 自定义组件
  • 基础教程
  • 能力教程
  • 性能优化
  • 安全
  • TTML-引用
    收藏
    我的收藏

    TTML 提供两种文件引用方式 import 和 include。

    import

    import可以在该文件中使用目标文件定义的template,如:
    在 item.ttml 中定义了一个叫 item 的template:
    <!-- item.ttml --> <template name="item"> <text>{{text}}</text> </template>
    在 index.ttml 中引用了 item.ttml,就可以使用 item 模板:
    <!-- index.ttml --> <import src="item.ttml" /> <template is="item" data="{{text: 'forbar'}}" />

    import 的作用域

    import 有作用域的概念,即只会 import 目标文件中定义的 template,而不会 import 目标文件 import 的 template。
    如:C import B,B import A,在 C 中可以使用 B 定义的 template,在 B 中可以使用 A 定义的 template,但是 C 不能使用 A 定义的 template。
    <!-- A.ttml --> <template name="A"> <text> A template </text> </template>
    <!-- B.ttml --> <import src="a.ttml" /> <template name="B"> <text> B template </text> </template>
    <!-- C.ttml --> <import src="b.ttml" /> <template is="A" /> <!-- Error! Can not use tempalte when not import A. --> <template is="B" />

    include

    include 可以将目标文件除了 <template/> 外的整个代码引入,相当于是拷贝到 include 位置,如:
    <!-- index.ttml --> <include src="header.ttml" /> <view> body </view> <include src="footer.ttml" />
    <!-- header.ttml --> <view> header </view>
    <!-- footer.ttml --> <view> footer </view>

    引用路径格式

    引用路径支持相对路径和绝对路径:
    <!-- index.ttml --> <!-- 相对路径,相对于当前文件 --> <import src="a.ttml" /> <import src="./a.ttml" /> <!-- 绝对路径,从项目根目录开始 --> <import src="/a.ttml" />
    ​