New line in speed directive

How to add a new line in a speed template using set? This does not work.

#(set $some = "$a \n $b")

Prints literally \ n.

Doing this also does not work:

VelocityContext context = new VelocityContext();
context.put("esc", new EscapeTool());
Velocity.evaluate(context, writer, "LOG", template);
+3
source share
2 answers

You can use $esc.neither its synonym $esc.newlinefrom EscapeTool for this:

#set($some = $a + $esc.n + $b)

How to run the tools:

ToolManager velocityToolManager = new ToolManager();
velocityToolManager.configure("velocity-tools.xml");
VelocityContext context = new VelocityContext(velocityToolManager.createContext());

You can get default-tools.xml by default from here (it is also included in the jar tool) and enable the necessary tools.

+4
source

You know that in modern versions of Velocity you just insert a line.

#set( $haslinebreak = "this has
a line break" )
+3
source

Source: https://habr.com/ru/post/1763787/


All Articles