# Default starting value of the loop
# counter variable reference.
directive.foreach.counter.initial.value = 1
2. 包含 #include 脚本元素允许模板设计人员包含(导入)本地文件, 这个文件将插入到#include 指令被定义的地方。文件的内容并不通过模板引擎来渲染。处于安全的原因,被包含的文件只可以放在TEMPLATE_ROOT下。
#include( "one.txt" ) #include 指令引用的文件在双引号内。如果超过一个文件,其间用逗号隔开。
#include( "one.gif","two.txt","three.htm" )
被包含的文件并不是一定要用文件名来引用,事实上,最好的办法是使用变量而不是文件名。这在根据规则决定何时提交页面时,决定目标输出是很有用的。
#include( "greetings.txt", $seasonalstock )
3. 解析 #parse 脚本元素允许页面设计员导入包含VTL的本地文件。 Velocity将解析和渲染指定的模板。
#parse( "me.vm" )
就象 #include 指令,#parse 可以使用变量而不是一个实在的模板文件。#parse 引用的模板文件必须包含的TEMPLATE_ROOT指定的目录之下。和 #include 指令不一样, #parse 只有一个参数。
VTL 模板templates can have #parse statements referring to templates that in turn have #parse statements. By default set to 10, the parse_directive.maxdepth line of the velocity.properties allows users to customize maximum number of #parse referrals that can occur from a single template. (Note: If the parse_directive.maxdepth property is absent from the velocity.properties file, Velocity will set this default to 10.) Recursion is permitted, for example, if the template dofoo.vm contains the following lines:
Count down.
#set( $count = 8 )
#parse( "parsefoo.vm" )
All done with dofoo.vm!
It would reference the template parsefoo.vm, which might contain the following VTL:
$count
#set( $count = $count - 1 )
#if( $count > 0 )
#parse( "parsefoo.vm" )
#else
All done with parsefoo.vm!
#end
After "Count down." is displayed, Velocity passes through parsefoo.vm, counting down from 8. When the count reaches 0, it will display the "All done with parsefoo.vm!" message. At this point, Velocity will return to dofoo.vm and output the "All done with dofoo.vm!" message.
4. 停止 #stop 脚本允许模板设计员停止模板引擎的执行,并返回。这通常用作调试。
#stop
1. 宏 #macro 脚本元素允许模板设计者在VTL 模板中定义重复的段。 Velocimacros 不管是在复杂还是简单的场合都非常有用。下面这个Velocimacro,仅用来节省击键和减少排版错误,介绍了一些Velocity宏的概念。
#macro( d )
#end
在例子中,Velocimacro定义为d,它可以象调用其他VTL指令一样的形式来进行调用:
#d()
当这个模板被调用时, Velocity 将 #d() 替换为一个单行的空表格。
Velocimacro 可以带一些参数,也可以不带参数(如上例所示)。但在他被调用时,所带的参数必须和其定义时的参数一样。很多Velocimacros 定义为不止一个参数。下面这个宏带有两个参数,一个颜色,一个数组。
#macro( tablerows $color $somelist )
#foreach( $something in $somelist )
$something
#end
#end