You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Scott Palmer <sc...@digital-rapids.com> on 2004/11/04 07:13:19 UTC

How to get a '#' in the output and a possible bug..

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>
...

But if I split that line of my template

$sys.thing(
##)  just a test


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

$sys.thing()

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?

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.


Regards,

Scott


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: How to get a '#' in the output and a possible bug..

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi all,

> You can use the EscapeTool:
>    http://wiki.apache.org/jakarta-velocity/EscapeTool
> $esc.s outputs "#".
Just changed it.  Now, $esc.h outputs "#".

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: How to get a '#' in the output and a possible bug..

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Scott,

## snip

> 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?
I'm not sure about the possible bug, but

> 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.
You can use the EscapeTool:
    http://wiki.apache.org/jakarta-velocity/EscapeTool
$esc.s outputs "#".

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <sh...@gmail.com>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: How to get a '#' in the output and a possible bug..

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Christoph,

> > What does the "H" stand for?  I'm the musical kind of guy, so I was
> 
> The name of it I often saw was "hash" or "hatchet", thus $H
Thanks a lot!  Here it is:
  http://dictionary.reference.com/search?q=hash%20character

Best regards,
-- Shinobu Kawai

-- 
Shinobu Kawai <sh...@gmail.com>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: How to get a '#' in the output and a possible bug..

Posted by Christoph Reck <ap...@recks.org>.
Shinobu Kawai wrote:
> Hi Christoph,
> 
>>#set( $H = '#' )
> 
> What does the "H" stand for?  I'm the musical kind of guy, so I was

The name of it I often saw was "hash" or "hatchet", thus $H

> using "S"harp.  Maybe "H" is better, because then, I can add

Since .NET days C# (C-Sharp) is a common term...

> "S"ingleQuote without confusion.  ;)

:D

> 
> Best regards,
> -- Shinobu Kawai

Cheers,
Christoph


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: How to get a '#' in the output and a possible bug..

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Christoph,

> #set( $H = '#' )
What does the "H" stand for?  I'm the musical kind of guy, so I was
using "S"harp.  Maybe "H" is better, because then, I can add
"S"ingleQuote without confusion.  ;)

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <sh...@gmail.com>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: How to get a '#' in the output and a possible bug..

Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Daniel,

> I'm very surprised to hear that \$foo.blah(\#) did not render as
> $foo.blah(#).  I'd call that a bug.
Actually, there's a whole bunch of stuff about escaping that I'm not
sure what is right.
    http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg13412.html

## That's why I came up with EscapeTool.  ;)

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <sh...@gmail.com>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: How to get a '#' in the output and a possible bug..

Posted by Daniel Rall <dl...@collab.net>.
On Thu, 2004-11-04 at 19:17 -0500, Scott Palmer wrote:
> On Nov 4, 2004, at 6:27 PM, Daniel Rall wrote:
> 
> > You can get a # character to render literally in your output by
> > backslash escaping it (e.g. \#), or in VTL code, by quoting it (e.g.
> > '#').
> >
> 
> No, the backslash escaping does not work.
> 
> I have a method that takes integers
> 
> $foo.blah(0)  would output "something"
> 
> I wanted to put that processing instruction literally in the output as 
> well so I could process the template and have the output show the 
> command syntax and what it generated.  So i tried
> \$foo.blah(\#)   to show that the method takes a number.
> The output was not what I expected for all combinations off 
> backslashing or not.
> I will try the quotes.

I'm very surprised to hear that \$foo.blah(\#) did not render as
$foo.blah(#).  I'd call that a bug.



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: How to get a '#' in the output and a possible bug..

Posted by Scott Palmer <sc...@digital-rapids.com>.
On Nov 4, 2004, at 6:27 PM, Daniel Rall wrote:

> You can get a # character to render literally in your output by
> backslash escaping it (e.g. \#), or in VTL code, by quoting it (e.g.
> '#').
>

No, the backslash escaping does not work.

I have a method that takes integers

$foo.blah(0)  would output "something"

I wanted to put that processing instruction literally in the output as 
well so I could process the template and have the output show the 
command syntax and what it generated.  So i tried
\$foo.blah(\#)   to show that the method takes a number.
The output was not what I expected for all combinations off 
backslashing or not.
I will try the quotes.

Scott


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: How to get a '#' in the output and a possible bug..

Posted by Daniel Rall <dl...@collab.net>.
You can get a # character to render literally in your output by
backslash escaping it (e.g. \#), or in VTL code, by quoting it (e.g.
'#').



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: How to get a '#' in the output and a possible bug..

Posted by Scott Palmer <sc...@digital-rapids.com>.
On Nov 4, 2004, at 4:07 AM, Christoph Reck wrote:

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

> #set ($D = '$' )
> #set( $H = '#' )
> ${D}sys.thing($H$H)   just a test
>
> will then properly output:
>
> $sys.thing(##)

I assumed that this sort of thing would work, but I fear it may be too 
complex for my users.
I was really hoping that the most straightforward case:

$sys.thing(#)

would just work.  Since the 'thing' method can't deal with a '#' and 
'#)' is not a valid VTL symbol it should output the string unchanged - 
right?  Why is the # not being spit out verbatim like it is for other 
input that doesn't match a VTL keyword?  e.g.:

The symbol # is called an octothorpe, hash, sharp, number sign, 
cross-hatch, square, or pound symbol. 
http://www.worldwidewords.org/weirdwords/ww-oct1.htm

In any case I found this really frustrating to deal with.  I think it 
should be handled better.

> 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.

It feels like a bug to me.  Isn't the parser supposed to leave 
unchanged anything it can't resolve as an object in the context?  In 
the first case above rather than throw the exception, I would expect 
maybe a log message and then the same as the second case - notice that 
it isn't really a method call.  In BOTH cases there is a unclosed 
method call.  Shouldn't an end-of-line comment and a true end of line 
work the same?

>> 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!

Isn't $sys.thing(#)  just as valid? Isn't # not treated special if what 
follows it is not a VTL keyword?

I wouldn't complain if it weren't for the fact that the reports I 
intend to generate with Velocity could very likely contain many '#'s in 
the output.  It's up to my end users to build the template, and they 
usually have no knowledge of programming.  I'm going to have to provide 
plenty of examples just to get them to deal with #foreach. I was 
thinking of telling them to always use the ${object} syntax just to 
avoid support issues.  Hey, it would be neat if I could FORCE Velocity 
to require that syntax, is it possible?

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

It certainly does require experimentation to get what you want. :)

Thanks for your help,

Scott


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org


Re: How to get a '#' in the output and a possible bug..

Posted by Christoph Reck <ap...@recks.org>.
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: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org