Use velocity's form of poor man's escaping, which will always work:

#set ($D = '$' )
$sys.thing($H$H)   just a test

will output (additionally a warning is written to the log file):

$sys.thing(##)


or even (if $sys is not a context object, or in your case, the method call will not take the ## as a valid parameter ):

#set ($D = '$' )
#set( $H = '#' )
${D}sys.thing($H$H)   just a test

will then properly output:

$sys.thing(##)

some more explanations embeded:

Scott Palmer wrote:
I'm new so if I just don't get it yet, sorry.

class MyObj {
 public String thing( int i ) { return String.valueOf( i*i ); }
}

MyObj sysObj = new MyObj();

context.add("sys",mySysObj);

My template looks like:

$sys.thing(##)   just a test

which results in a crash.
org.apache.velocity.exception.ParseErrorException: Encountered " \t" at line 18, column 11
Was expecting one of:
<EOF>
...

The parser wants the method call to be closed; but the line comment gobbled the closing bracket; therefore, the velocity construct is incomplete and the parser chokes.


But if I split that line of my template

$sys.thing(
##)  just a test

here the parser is a bit wiser, and notices that the construct is not a method call and emits the string. The closing bracket is part of the line comment and does not appear in the output.


or if I simply move the ## over by one character

$sys.thing()##  just a test

The template is processed without any problems. giving the output

this is 100% valid VTL!


$sys.thing()

But you probably are getting a warning message in the log file.


Why is the single line comment not working when it is between the parenthesis, when a true end of line at the same spot works fine?

explained above.


I discovered this while trying to output the line $sys.thing(#)

to show the usage of the sys.thing method within the template. I've been trying to escape the $ or # with a backslash, but nothing give me what I want so that no substitution takes place and I don't get a backslash included in the output.

Use the ${H} form to insert the "#" string during rendering, making sure the parser is not seing the marker. Same applies for context object references using a ${D} instead of a simple $refName.

Escaping with a backslash \$ only works partially in velocity.
You may call it a "broken" feature.

\$sys

outputs (in your case because its in the context):

$sys

whereas

\$foo

will emit:

\$foo

since $foo is not in the context, and the velocity principle is
to leave unchanged anything it does not know about.



Regards,

Scott

Please ask if something was not clear in my explanations.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to