I need to access a value of a Velocity template's variable named:
$myFeature.enabled
Mind the dot within the name. It is defined in the code as:
myFeature.enabled=true
The problem is that, when I want to get the value of that variable withing html context with the following expression:
'${myFeature.enabled}'
it is not resolved and just gives:
"${myFeature.enabled}"
I tried to escape the dot with \ or change apostrophes to " but without luck.
Changing the name of the variable to a one without a dot is not an easy option for various reasons, so please suggest any other solutions.
I need to access a value of a Velocity template's variable named:
$myFeature.enabled
Mind the dot within the name. It is defined in the code as:
myFeature.enabled=true
The problem is that, when I want to get the value of that variable withing html context with the following expression:
'${myFeature.enabled}'
it is not resolved and just gives:
"${myFeature.enabled}"
I tried to escape the dot with \ or change apostrophes to " but without luck.
Changing the name of the variable to a one without a dot is not an easy option for various reasons, so please suggest any other solutions.
Share Improve this question asked May 27, 2014 at 12:53 BlaiseBlaise 7,5187 gold badges46 silver badges54 bronze badges3 Answers
Reset to default 10The answer is as simple as:
Velocity variable naming: does not allow dots within variable
Also here:
To use a $SINGLE.VARIABLE.IDENTIFIER.WITH.DOTS no backslash is required. The engine will not treat such expression as a variable to be processed because a Velocity variable cannot contain dots according to the Velocity variable notation.
Links:
- http://velocity.apache/engine/devel/vtl-reference-guide.html
- http://www.jetbrains./idea/webhelp10.5/escaping-characters-in-velocity-variables-and-directives.html
According to the Velocity variable notation, a variable cannot contain dots.
What you can do to work-around the issue, is to add the velocity context itself to the context:
VelocityContext context = new VelocityContext();
context.put("globals", context); // <-- TADA
context.put("myFeature.enabled", Boolean.TRUE);
// ...
then from the template, do:
#if ($globals.get('myFeature.enabled'))
...
#end