您的位置:首页 > 其它

Velocity入门指南——第八章 导入外部文件

2015-12-04 00:00 274 查看
摘要: Velocity入门指南 导入外部文件

Velocity有两种方式从外部引入文件:包括和解析。

1 包括

#include脚本元素允许模板设计者导入本地文件,然后插入到#include指令定义的位置。文件的内容通过模板引擎渲染。由于安

全原因,文件只能包含在TEMPLATE_ROOT。

#include("one.txt")

#include指令引用的文件被封闭在双引号中。如果多个文件被include,应该通过逗号分隔。

#include("one.gif","two.txt","three.htm")

可以使用变量替换文件名作为参数。

#include("greetings.txt", $seasonalstock)

2 解析

#parse脚本元素允许模板设计者导入包含VTL的本地文件。Velocity将解析VTL并渲染模板。

#parse("me.vm")

像#include指令一样,#parse可以传入一个变量而不是模板。任意引用的模板必须在TEMPLATE_ROOT中。和#include指令

不同,#parse只需要一个参数。

VTL模板可以在模板中递归使用#parse引用。velocity.properties中的directive.parse.max.depth属性允许用户自定义最大递

归数,默认设置为10。(如果velocity.properties文件中没有出现directive.parse.max.depth,Velocity将默认设置为10)。

Count down.

#set( $count = 8 )

#parse( "parsefoo.vm" )

All done with dofoo.vm!

引用的模板:

$count

#set( $count = $count - 1 )

#if( $count > 0 )

#parse( "parsefoo.vm" )

#else

All done with parsefoo.vm!

#end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息