MrUll's

Juni 28, 2005

Jakarta Velocity Tips

i collected some helpful tips after some weeks of coding using the jarkarta
velocity template engine. See theVelocity Homepage for Docs aso. These tips require some knowledge within the velocity template language (VTL)

  • a "Quiet Reference Notation" produces an emtpy String in case of a non existance which evaluates to true if used in an if statement. Snippet: #if($!value) ...
  • this init expression has a correct syntax, produces no runtime error but seems to leave actual values persistent for several parse calls. #set(${type}=false)
  • A nullvalue assignment preserves the original value as an assigment does not take place. Example: #set($type=$tool.getValueFor($key)). an explicit init (false) has to be done for $type before to make the code robust. especially in large files where $type is used a lot and when many parse calls take place upon the same vm-template such cases are likely to produce an error
  • there are some settings which affect velocity macros during runtime. the default settings causes macros to be loaded as global and subsequent macro definitions (e.g. from other templates) are rejected if the name is the same. these events are usually logged in the velocity log file.
  • errors during if-evaluations occurr when different objects are to be compared (e.g. Boolean and String). these events are also logged but rendering of the templates goes on.
  • its important to use blanks if you do some arithmetics. For example #set($var=$old+1) is wrong; #set($var=$old + 1) does a correct job.