Velocity Templates - New Line
Solution 1
We had issues with newlines and ended up putting a property on the VelocityContext:
VelocityContext ctx = new VelocityContext();
ctx.put("newline", "\n");
Then, wherever we needed to use a newline, we would reference the context variable:
$newline
We use this in cases where we need to replace newlines in a string with <br />.
Solution 2
I needed a new line for generating javascript. Well, I didn't need it of course, but it made reading the generated code easier while developing. In this case, I just set a variable so that the Velocity was easier to read. This is all you need:
Velocity Code:
#set( $newline="
")
#set( $jsCode = "var bling='blang';{$newline}var bark='bite';{$newline}" )
<script>
$jsCode</script>
Result:
<script>
var bling='blang';
var bark='bite';
</script>
Solution 3
Are you using Velocity to generate HTML content? In that case remember that you need to use <br>
not a newline.
If you actually want a new line character you just put the actual new line character, i.e. press enter. There is no escape sequences like \n in Velocity.
Solution 4
If you are using Velocity 1.5 or later, you can also just put the new line in there:
#set( $foo = "this has a
line break" )
Solution 5
In Velocity 1.4 neither newlines in strings works nor does the escapeTool.getNewline()
exist. If you do not have the possibility to add anything to the context, here is a Velocity-only hack to generate arbitrary characters:
#macro(chr $charCode $str)
#set($str="0")
#set($chars=$str.charAt(0).toChars($charCode))
#set($str="")
#foreach($char in $chars)
#set($str="$str$char")
#end
#end
#chr(10 $nl)
First Line${nl}Second Line
It uses Character.toChars() to convert the ASCII code 0x0A = 10
to a char[]
which it then assembles into a string. This way one could generate any character such as
#chr(129299 $nerd) ## U+1F913 => 0x1F913 = 129299
$nerd
## outputs 🤓
LdSe
Updated on July 11, 2022Comments
-
LdSe almost 2 years
I've been working with Apache's Velocity engine and a custom template.
The thing is, that I haven't been able to generate a String with the corresponding line breaks. I tried almost everything that I found, such as using $esc.n and $esc.newline (I'm already using escape tools on my project) but it seems that the version I'm currently using doesn't support it (1.4), checked if putting '\n', '\\n' and even '\\\n' would work, but same thing.Does anyone have any solution to this?
-
LdSe over 13 yearsI'm using Velocity to generate XML content. Also, that feature about using enter to add a new line, is a behaviour of latest versions.
-
LdSe over 13 yearsI'll try with the context solution. Will provide an update on the results. Thanks a lot!
-
LdSe over 13 yearsIt worked! Thank you so much for your help :). For anyone wondering, I simply added that property to the context, and referenced it in the template just like you instructed.
-
Toseef Zafar over 7 yearsHi I am using this to get the body of the email: String body = VelocityEngineUtils.mergeTemplateIntoString( velocityEngine, filename, params); in params I have param.put("newline", "\n") and in velocity template (the .vm file) I am using it like this $newline but doesn't seem to work, any ideas?
-
jt. over 7 years@TouseefZafar Unfortunately, it has been several years since I have used Velocity, so I am probably not of much help now. The one thing that stands out to me is (presumably) your "params" object is a Map. Is that equivalent to the Velocity Context? If not, this solution may not work.
-
simba over 5 yearsI am facing a similar problem.. I have a form where i enter comment text and send an email.. however if the use has pressed enter(newline) that does not appear in the email.. comment text appears in a single line