Velocity and ScriptContext synchronization

Last modified by Vincent Massol on 2021/04/06

VelocityContext and ScriptContext are now fully in sync (and if you find a use case where it's not true create a BUG on and VelocityContext is now close to internal detail. This allows for example defining a variable in Velocity and accessing it from a Groovy script:

## Setting some script binding in Velocity
#set($myvar = "toto")

// Lets use the script binding that has been set in previous script
print myvar

In the past the opposite was already possible (defining a variable in Groovy or in any JSR223 scripting language and accessing it from Velocity), but there were some limitations. For example it was impossible to overwrite in groovy a binding that was already in the VelocityContext. Since the VelocityContext and ScriptContext were not fully in sync, it was leading to many hack having to deal with both (and often forgetting one of the other).

From now on if you want to set some binding for whatever script is going to be executed next you should to it through ScriptContext and completely forget that VelocityContext ever existed in most cases.

New APIs to help with that:

  • VelocityManager#evaluate(Writer out, String templateName, Reader source) which execute the passed velocity content with the passed template name and write the result in the passed Writer. This method deal with VelocityContext internally
  • ScriptContextManager#getCurrentScriptContext() should be used when you want to SET something in the current script context so that it end up in a script that might be executed later
  • ScriptContextManager#getScriptContext() is not new but should be used only if you want to get the ScriptContext exactly as a script would get it (to see if it contains something you want to access), this should be a rare use case.

Get Connected