Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a velocity template that outputs two '#' litterals at the beginning of the line?

Tags:

java

velocity

I need to create a velocity document that has some lines prefixed with ¨## Foo", but since the # is the velocity directive command, it is not working for me.

What I want my output document to look like:

## Foo this
## Bar that
k1, v1
k2, v2
k3, v3

Escaping has not worked out how I expected. The below does not work, obviously, as the #s are un-escaped):

## Foo this
## Bar that
#foreach( $foo in $bar )
$foo.key, $foo.value
#end

The ## lines don't show up--again, as expected. But, my attempts at escaping do not work either. Escaping solution one:

\## Foo this
\## Bar that
#foreach( $foo in $bar )
$foo.key, $foo.value
#end

or this, escaping solution two:

\#\# Foo this
\#\# Bar that
#foreach( $foo in $bar )
$foo.key, $foo.value
#end

or even this...

# set($msg = "##") 
$msg Foo this
$msg Bar that
#foreach( $foo in $bar )
$foo.key, $foo.value
#end

This last one really confused me.

Any ideas?

like image 891
Stu Thompson Avatar asked Dec 06 '25 05:12

Stu Thompson


2 Answers

Use the Velocity context:

${esc.h}${esc.h}
like image 114
cadrian Avatar answered Dec 07 '25 20:12

cadrian


If you don't want to add org.apache.velocity.tools.generic.EscapeTool or similar to your context, you can use a slightly modified version of your third try:

#set($msg = "#") 
${msg}${msg} Foo this
like image 44
Simon Nickerson Avatar answered Dec 07 '25 18:12

Simon Nickerson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!