Using Syntax and Conditions in Templates

Syntax

Together uses Velocity to generate templates. Velocity is a Java-based template engine. This page describes commonly used Velocity syntax.

Variables

Notation:

$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]

Notation examples:

Normal: $long-Thrower_4
Silent: $!long-Thrower_4
Formal: ${long-Thrower_4}

If /ElseIf /Else Conditionals

Format:

#if( [condition] ) [output] [ #elseif( [condition] ) [output] ]* [ #else [output] ] #end

Usage:

condition: If Boolean, considered true when its value is true. If not Boolean, considered true when not null.
output: May contain Velocity Template Language.

Examples:

Equivalent Operator: #if( $foo == $bar )
Greater Than: #if( $foo > 34)
Less Than: #if( $foo < 34 )
Greater Than or Equal To: #if( $foo >= 34 )
Less Than or Equal To: #if( $foo <= 34 )
Equals Number: #if( $foo == 34 )
Equals String: #if( $foo == "bar" )

Foreach Loop

Format:

#foreach( $ref in arg ) statement #end

Usage:

$ref: The first variable reference.
arg: May be a reference to a list (i.e. object array, collection, or map), an array list, or the range operator.
statement: The output when Velocity finds a valid item in the arg list. Output is any valid Velocity Template Language statement. Rendered each iteration of the loop.

Examples:

Reference: #foreach ( $part in $whole )
Array list: #foreach ( $part in ["Follow", $my, "lead"] )
Range operator: #foreach ( $part in [1..3] )

This example of a foreach loop is on the Units tab in the Unit Test package template in Together:

For a thorough syntax list and explanations, visit: http://jakarta.apache.org/velocity/vtl-reference-guide.html

NB: VTL directives (beginning with "#") are displayed in the snippets area as comments.

Conditions

When using the template editor to write the code snippets and syntax for fields, methods and classes, you can write conditional logic to control what is created when the template is applied.

The following example shows how conditional logic applies to the Applet template:

  1. From the menubar, select Window > Show View > Other > Together Patterns > Templates to display the Templates view.


  2. Expand the Local Templates > Java Package > Standard.





  3. Double click on the Applet template to open the template editor.
     

  4. Click on the Variables tab. Notice the Boolean variable named init, which controls the code generation by the Applet template.




  5. Click on the Units tab. Notice that init, the Boolean variable declared on the Variables tab, controls the creation of the methods init() and destroy().



The typical format for conditional statements is as follows:

#if([${reservedVariable}] | [${variable from Variable Tab}] [operator] [operand])
...template code to be generated is inserted here...
#end

When you type the $ character as you write a condition, a pop up window appears allowing you to select one of the current variables, including the reserve variables applicable for this type of template. This variable selected is inserted into the template snippet at the cursor.

An if condition can be anywhere within the template code; they are granular down to specific lines of code.

NB: Variable names declared on the Variables tab must be preceded by a $ symbol. Otherwise, they are treated as normal text.