The #parse
script element allows the template designer to import a local
file that contains VTL. Velocity will parse the VTL and render the template specified.
#parse( "me.vm" )
Like the #include
directive, #parse
can take a variable
rather than a template. Any templates to which #parse
refers must be
included under TEMPLATE_ROOT. Unlike the #include
directive,
#parse
will only take a single argument.
VTL templates can have #parse
statements referring to templates that in
turn have #parse
statements. By default set to 10, the
directive.parse.max.depth
line of the
velocity.properties
allows users to customize maximum number of
#parse
referrals that can occur from a single template.
directive.parse.max.depth
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.