I started looking into Velocity for templating, however I can't seem to escape the dot character after a variable name. Below is the template I have.
## function.vm
void functionA() {
int minimum_value = $class_name.MIN_VALUE;
int maximum_value = $class_name.MAX_VALUE;
}
Where $class_name
is a variable that I set in the VelocityContext
as below.
VelocityContext velocityContext = new VelocityContext();
velocityContext.put("class_name", "Short");
However, this would output the below, no change.
void functionA() {
int minimum_value = $class_name.MIN_VALUE;
int maximum_value = $class_name.MAX_VALUE;
}
Adding a back-slash before the dot character $class_name\.MIN_VALUE
would replace the variable, but it would also print the back-slash as well, as below.
void functionA() {
int minimum_value = Short\.MIN_VALUE;
int maximum_value = Short\.MAX_VALUE;
}
Is escaping not done with a back-slash? I found multiple answers saying it should be with a back-slash, others with using $esc.java()
but that didn't work as well.
CodePudding user response:
You can use curly-braces around class_name
like:
## function.vm
void functionA() {
int minimum_value = ${class_name}.MIN_VALUE;
int maximum_value = ${class_name}.MAX_VALUE;
}
This generates the below output:
void functionA() {
int minimum_value = Short.MIN_VALUE;
int maximum_value = Short.MAX_VALUE;
}