Velocity Templates with Java Calls - Tradeoffs | ModelPro

4 replies [Last post]

This topic explores the tradeoffs involved where Velocity Templates within a cartridge become complex enough to warrant calling out to Java. Apache Velocity is an open source software project directed by the Apache Software Foundation. Velocity is a Java-based template engine that provides a simple yet powerful template language and is becoming popular for use both at build-time and run-time. While particularly powerfull within the general context of template processing, Velocity is not a full-featured language, and some tasks can be extreemly cumbersome. For instance simple alphanumeric string processing tasks such as capitalization related routines seem to be far more effectively implemented in pure Java. Velocity and the ModelPro template binding infrastructure provide a means of declaring variables in a binding specification which can be used "in-line" within Velocity based templates or Velocity Macros. As the number of such utilities grows within a large cartridge project, how do we keep a clean seperation between the types of utilities such that they are easily understood, maintained and re-used within other projects? Are there restrictions to which such utilities must adhere? What are the basic categories of such utilities?

 

 

 

 

break example

See http://velocity.apache.org/engine/releases/velocity-1.6.2/user-guide.html.

## list first 5 customers only
#foreach( $customer in $customerList )
#if( $velocityCount > 5 )
#break
#end
$customer.Name
#end

Java "call-out" categories

Here's an initial list of rough categories where Java call outs from Velocity make sense.

1.) String Manipulation
2.) Sorting
3.) Type Checking and Conversion
4.) Package Name Checking and Manipulation

agree

Ya that kind of limitation is what gives rise to pushing code into Java. But you can at least check and set various directives related to iteration. See:

http://velocity.apache.org/engine/devel/user-guide.html#Loops

User offline. Last seen 50 weeks 3 hours ago. Offline
Joined: Mar 25 2009
Groups: ModelPro
Velocity iteration

Hi; I'm working on a custom cartridge and am studying up on Velocity. The iteration cabability looks extreemly limited. For instance there is a #foreach but no #while loop, and how do you break out of a loop??