jetbrick-template-1.x 旧版文档请看这里:http://subchen.github.io/jetbrick-template/1x/

§嵌入子模板 #include

jetbrick-template 中,我们经常需要在一个模板中嵌入另一个模板。这时候,我们可以使用下面的指令:

#include(file, [parameters], [returnName])

§1. 常规的做法

§2. 父模板传递参数给子模板

§3. 子模板返回对象给父模板

除了父模板可以传递参数给子模板,子模板也能返回对象给父模板。

这里就需要使用 #return 指令。

看例子:

父模板:/parent.jetx

#define(Object RESULT)

#include("/sub.jetx", {}, "RESULT")
${RESULT}

子模板:/sub.jetx

This is a sub template.
#return( [2,4,6,8] )

结果:

[2,4,6,8]

提示

  • #return 指令可以返回任意对象,默认存储在父模板 context 中,变量名由 #include 指令的第三个参数 returnName 所指定。
  • #return 指令后面的语句不会被执行,所以应该把 #return 放在子模板的最后面
  • 如果 #include 第三个参数 returnName 没有指定,那么默认为 RESULT