You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Peter Mutsaers <pe...@mutsaers.com> on 2001/07/26 16:33:26 UTC

VTL: How to escape "in a string?

Hello,

I need to do:
#set ($tevdatesel = "<select name="date">$tevdates.OptionList</select>")

But obviously this causes an error since the " just before date
terminates the string in VTL.

Trying the obivous, using \ to escape the string (\"date\") somehow
works (rather mysteriously) but cases the output to have \"date\"
literally, that is, the escape character is not removed.

Is this a bug?

Is there another (handy) way to do this, i.e. without using multiple
#sets to build the string in multiple steps (which would be a pain
#especially if one has to mix many " and ' characters in the same
#string).



-- 
Peter Mutsaers  |  Dübendorf    | UNIX - Live free or die
plm@gmx.li      |  Switzerland  | Sent via FreeBSD 4.3-stable

RE: How to escape "in a string?

Posted by Tal Dayan <ta...@zapta.com>.
Oops, it should be 'a goot reason NOT to ...' ;-)

Tal

> -----Original Message-----
> From: gmj@mta6.srv.hcvlny.cv.net [mailto:gmj@mta6.srv.hcvlny.cv.net]On
> Behalf Of Geir Magnusson Jr.
> Sent: Thursday, July 26, 2001 3:24 PM
> To: velocity-user@jakarta.apache.org
> Subject: Re: How to escape "in a string?
> 
> 
> Tal Dayan wrote:
> > 
> > BTW, is there a good reason why to to allow esacping the '"' 
> and "'" chars
> > such as
> > \" and \' when the context is a #set directive (and similar cases) ?
> > 
> 
> Its definately *a* good reason - the only question is do we want to add
> this to VTL (which makes it more complicated - arguably not for people
> who are used to escaping...), or let people use the other solutions...
> 
> geir
> 
> -- 
> Geir Magnusson Jr.                           geirm@optonline.net
> System and Software Consulting
> Developing for the web?  See http://jakarta.apache.org/velocity/
> Be well, do good work, and keep in touch.
> 

Re: How to escape "in a string?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Tal Dayan wrote:
> 
> BTW, is there a good reason why to to allow esacping the '"' and "'" chars
> such as
> \" and \' when the context is a #set directive (and similar cases) ?
> 

Its definately *a* good reason - the only question is do we want to add
this to VTL (which makes it more complicated - arguably not for people
who are used to escaping...), or let people use the other solutions...

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

RE: How to escape "in a string?

Posted by Tal Dayan <ta...@zapta.com>.
When it comes to HTML pages, you can also use a single quote such as

#set($aaa =  "<a href='$url'><font color='$color'>$name<font></a>")

This is valid according to
http://www.htmlhelp.org/reference/html40/structure.html#attributes

Yes, I know, this is a limited solution but it addresses many of the cases
we
encountered. Also, you can try to define a $dq variable with the value '"'
and use
it instead.

BTW, is there a good reason why to to allow esacping the '"' and "'" chars
such as
\" and \' when the context is a #set directive (and similar cases) ?

Tal

> -----Original Message-----
> From: Warner Onstine [mailto:warner@warneronstine.com]
> Sent: Thursday, July 26, 2001 9:36 AM
> To: velocity-user@jakarta.apache.org
> Subject: Re: How to escape "in a string?
>
>
> And it strikes again =).
>
> Here was the answer I got from an earlier post:
>
> > Hi all,
> > I'm just hoping there's a better solution to this. I am trying
> to do this:
> >
> > #set ($catLink = "<a href='$linkStr'>$category.Name</a>")
> >
> > But, I don't want the href to contain single quotes, but instead double
> > quotes. I've looked through the docs and haven't found anything
> related to
> > this.
> >
> > Everything that I've tried so far has either resulted in a bad
> link or an
> > error, any help would be appreciated.
> >
> > -warner
>
> #macro ( link $name $value )<a href="$value">$name</a>#end
>
> #set ($catLink = "#link($category.Name $linkStr)")
>
> Or, if you are using Turbine, just use $link
>
> -jon
>
> -----------------
> -warner
> ----- Original Message -----
> From: "Peter Mutsaers" <pe...@mutsaers.com>
> To: <ve...@jakarta.apache.org>
> Sent: Thursday, July 26, 2001 7:33 AM
> Subject: VTL: How to escape "in a string?
>
>
> > Hello,
> >
> > I need to do:
> > #set ($tevdatesel = "<select name="date">$tevdates.OptionList</select>")
> >
> > But obviously this causes an error since the " just before date
> > terminates the string in VTL.
> >
> > Trying the obivous, using \ to escape the string (\"date\") somehow
> > works (rather mysteriously) but cases the output to have \"date\"
> > literally, that is, the escape character is not removed.
> >
> > Is this a bug?
> >
> > Is there another (handy) way to do this, i.e. without using multiple
> > #sets to build the string in multiple steps (which would be a pain
> > #especially if one has to mix many " and ' characters in the same
> > #string).
> >
> >
> >
> > --
> > Peter Mutsaers  |  Dübendorf    | UNIX - Live free or die
> > plm@gmx.li      |  Switzerland  | Sent via FreeBSD 4.3-stable
> >
>
>


Re: How to escape "in a string?

Posted by Warner Onstine <wa...@warneronstine.com>.
And it strikes again =).

Here was the answer I got from an earlier post:

> Hi all,
> I'm just hoping there's a better solution to this. I am trying to do this:
>
> #set ($catLink = "<a href='$linkStr'>$category.Name</a>")
>
> But, I don't want the href to contain single quotes, but instead double
> quotes. I've looked through the docs and haven't found anything related to
> this.
>
> Everything that I've tried so far has either resulted in a bad link or an
> error, any help would be appreciated.
>
> -warner

#macro ( link $name $value )<a href="$value">$name</a>#end

#set ($catLink = "#link($category.Name $linkStr)")

Or, if you are using Turbine, just use $link

-jon

-----------------
-warner
----- Original Message -----
From: "Peter Mutsaers" <pe...@mutsaers.com>
To: <ve...@jakarta.apache.org>
Sent: Thursday, July 26, 2001 7:33 AM
Subject: VTL: How to escape "in a string?


> Hello,
>
> I need to do:
> #set ($tevdatesel = "<select name="date">$tevdates.OptionList</select>")
>
> But obviously this causes an error since the " just before date
> terminates the string in VTL.
>
> Trying the obivous, using \ to escape the string (\"date\") somehow
> works (rather mysteriously) but cases the output to have \"date\"
> literally, that is, the escape character is not removed.
>
> Is this a bug?
>
> Is there another (handy) way to do this, i.e. without using multiple
> #sets to build the string in multiple steps (which would be a pain
> #especially if one has to mix many " and ' characters in the same
> #string).
>
>
>
> --
> Peter Mutsaers  |  Dübendorf    | UNIX - Live free or die
> plm@gmx.li      |  Switzerland  | Sent via FreeBSD 4.3-stable
>


Re: VTL: How to escape "in a string?

Posted by David Kinnvall <da...@alertir.com>.
From: "Geir Magnusson Jr." <ge...@optonline.net>
> David Rees wrote:
> >
> > > -----Original Message-----
> > > From: gmj@mta6.srv.hcvlny.cv.net [mailto:gmj@mta6.srv.hcvlny.cv.net]On
> > >
> > > No, it's an 'incomplete'.  I added the support for \" in the parser,
but
> > > never added the code to support it, wanting to see what the community
> > > wanted.
> > >
> > > I think that the general sentiment would be to put the last bit in,
eh?
> >
> > >From a programmers perspective it certainly makes sense and makes life
a lot
> > easier for me when I'm writing templates.
> >
>
> I agree, but you also mentioned what I think is the 'rub' of this,
> namely 'From a programmers perspective..."  Thats my worry, anyway, that
> we are blinded by our programmer hats...

I must say that the examples of other solutions to the
issue at hand feels very awkward, to me at least. Having
to use additional variables to achieve the same result
as a 'simple' escape, isn't that even more programming-
ish? I mean, it makes the template harder to read and
follow for *me*, what about my template designers? :-)

After thinking through it once more, I feel pretty certain
that the non-programmers and designers at my place will have
a much harder time understanding any of the alternatives
presented so far than the escape-variant, programmer-ish
or not.

Again, that's just a feeling of mine...

Regards,

David.


Re: VTL: How to escape "in a string?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Rees wrote:
> 
> > -----Original Message-----
> > From: gmj@mta6.srv.hcvlny.cv.net [mailto:gmj@mta6.srv.hcvlny.cv.net]On
> >
> > No, it's an 'incomplete'.  I added the support for \" in the parser, but
> > never added the code to support it, wanting to see what the community
> > wanted.
> >
> > I think that the general sentiment would be to put the last bit in, eh?
> 
> >From a programmers perspective it certainly makes sense and makes life a lot
> easier for me when I'm writing templates.
> 

I agree, but you also mentioned what I think is the 'rub' of this,
namely 'From a programmers perspective..."  Thats my worry, anyway, that
we are blinded by our programmer hats...

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

RE: VTL: How to escape "in a string?

Posted by David Rees <dr...@ebetinc.com>.
> -----Original Message-----
> From: gmj@mta6.srv.hcvlny.cv.net [mailto:gmj@mta6.srv.hcvlny.cv.net]On
>
> No, it's an 'incomplete'.  I added the support for \" in the parser, but
> never added the code to support it, wanting to see what the community
> wanted.
>
> I think that the general sentiment would be to put the last bit in, eh?

>From a programmers perspective it certainly makes sense and makes life a lot
easier for me when I'm writing templates.

-Dave


Re: VTL: How to escape "in a string?

Posted by Jeremy Leader <jl...@alumni.caltech.edu>.
At 11:15 PM 7/26/01 , Peter Mutsaers wrote:
>B.t.w., while talking about string literals: How do I get a \n
>(newline) in a string literal?
>
>If I write:
>#set($strwithnl = "hello this
>is a string") I get an error. I don't think I can use \n, since the
>docs say nothing about it. Can I use a \ before the EOL?

I thought I had a way to do this, similar in spirit to the $quote
trick mentioned before, but it won't work:

#macro (nl)
#end

The trouble is, as I understand it, macros don't really have a return
value; they just write to the output.  So this won't work:

#set($strwithnl = "hello this#{nl}is a string")

You could put a $nl into the context in your Java code.

Jeremy


Re: schmoo? RE: VTL: How to escape "in a string?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
David Rees wrote:
> 
> > -----Original Message-----
> > From: gmj@mta4.srv.hcvlny.cv.net [mailto:gmj@mta4.srv.hcvlny.cv.net]On
> > >
> > > Would \" turn into " only in "values", that is things being
> > > manipulated by Velocity, like inside directives and in
> > > macro parameters?  Or would the translation occur even in
> > > schmoo?
> >
> > Never in schmoo.  Shmoo is sacred. Schmoo is holy.
> >
> > (The more we write 'schmoo', the better.)
> 
> Wait, wait, wait a second here.  ;-)  Is it shmoo? Or schmoo?  Better get it
> consistent if you want it to stick.  ;-)
> 
> And what the heck is schmoo again?

I noticed that after I sent it... 'schmoo'

Schmoo is anything that isn't a directive or a reference.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

schmoo? RE: VTL: How to escape "in a string?

Posted by David Rees <dr...@ebetinc.com>.
> -----Original Message-----
> From: gmj@mta4.srv.hcvlny.cv.net [mailto:gmj@mta4.srv.hcvlny.cv.net]On
> >
> > Would \" turn into " only in "values", that is things being
> > manipulated by Velocity, like inside directives and in
> > macro parameters?  Or would the translation occur even in
> > schmoo?
>
> Never in schmoo.  Shmoo is sacred. Schmoo is holy.
>
> (The more we write 'schmoo', the better.)

Wait, wait, wait a second here.  ;-)  Is it shmoo? Or schmoo?  Better get it
consistent if you want it to stick.  ;-)

And what the heck is schmoo again?

-Dave


Re: VTL: How to escape "in a string?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Jeremy Leader wrote:
> 
> At 03:29 PM 7/26/01 , Geir Magnusson Jr. wrote:
> >Peter Mutsaers wrote:
> > >
> > > Trying the obivous, using \ to escape the string (\"date\") somehow
> > > works (rather mysteriously) but cases the output to have \"date\"
> > > literally, that is, the escape character is not removed.
> > >
> > > Is this a bug?
> >
> >:)
> >
> >No, it's an 'incomplete'.  I added the support for \" in the parser, but
> >never added the code to support it, wanting to see what the community
> >wanted.
> >
> >I think that the general sentiment would be to put the last bit in, eh?
> >
> >geir
> 
> I'd love to have this (in fact I pulled my hair out wrestling
> with this in hrefs when I first started with Velocity), but I
> have a few questions about the details of how it would work.
> 
> Would \" turn into " only in "values", that is things being
> manipulated by Velocity, like inside directives and in
> macro parameters?  Or would the translation occur even in
> schmoo?

Never in schmoo.  Shmoo is sacred. Schmoo is holy.

(The more we write 'schmoo', the better.)

No - it would only occur in things that syntactially are string literals
to Velocity, so that would only include :

- arguments to directives

   #foo( "hello" )

- arguments to methods

  $foo.bar("Hello")

- RHS of assignement in #set()

  #set( $foo = "hello" )

(which syntactically is no different than an arg to directive)

> 
> If the latter (\" becomes " everywhere) how would I put a single
> backslash in the middle of schmoo?  Would I have to double it?
> And how would I represent a backslash and a quote?

It wouldn't be in shmoo - Velocity should be able to take any content
and parse and render it identically to the input, with the exception of
the reserved directives.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

Re: velocity.properties symbol question

Posted by Jon Stevens <jo...@latchkey.com>.
on 7/27/01 6:59 PM, "Nick Bauman" <ni...@cortexity.com> wrote:

> Didn't see this in the javadoc, but what (does / is) this (do / used for)?
> If it's on the site somewhere, my apologies in advance:
> 
> runtime.log.logsystem.log4j.remote.host

It is part of the log4j API to allow you to send Logging objects to a remote
server. Read up on log4j.

-jon


velocity.properties symbol question

Posted by Nick Bauman <ni...@cortexity.com>.
Didn't see this in the javadoc, but what (does / is) this (do / used for)? 
If it's on the site somewhere, my apologies in advance:

runtime.log.logsystem.log4j.remote.host

-- 
Nick Bauman
Software Engineer
3600 Dupont
Minneapolis, MN
55412
Mobile Phone: (612) 810-7406
Home Phone: (612) 522-0165


RE: VTL: How to escape "in a string?

Posted by Paulo Gaspar <pa...@krankikom.de>.
\t and the other usual escapes would also be nice for some less usual
templates (data exports, code generation, etc.).

Have fun,
Paulo

> -----Original Message-----
> From: gmj@mta6.srv.hcvlny.cv.net [mailto:gmj@mta6.srv.hcvlny.cv.net]On
> Behalf Of Geir Magnusson Jr.
> 
> ...
>
> When we get the \" and \', we can add \n as well.
> 
> -- 
> Geir Magnusson Jr.                           geirm@optonline.net


Re: VTL: How to escape "in a string?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Peter Mutsaers wrote:
> 
> >> "Jeremy" == Jeremy Leader <jl...@alumni.caltech.edu> writes:
> 
>     Jeremy> If the latter (\" becomes " everywhere) how would I put a
>     Jeremy> single backslash in the middle of schmoo?  Would I have to
>     Jeremy> double it?  And how would I represent a backslash and a
>     Jeremy> quote?
> 
> I'd hope so. Anything else is messy. Everywhere the escape character
> can be obtained by doubling it.
> 
> B.t.w., while talking about string literals: How do I get a \n
> (newline) in a string literal?
> 
> If I write:
> #set($strwithnl = "hello this
> is a string") I get an error. I don't think I can use \n, since the
> docs say nothing about it. Can I use a \ before the EOL?

When we get the \" and \', we can add \n as well.

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

Re: VTL: How to escape "in a string?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Tal Dayan wrote:
> 
> We plan to implement a report generator with Velocity and it will generate
> among other things MS Word files in RTF format. RTF format uses backslash
> (\) characters intensively (similar to '/' in HTML/XML).
> 
> Will we have to escape each of them or does a something like {\xyz... in the
> shmoooo
> will be preserved as is ? What about the proposed escaping feature, will it
> change it ?
> 

\ in schmoo is a \



> Thanks,
> 
> Tal
> 
> > -----Original Message-----
> > From: plm@mutsaers.com [mailto:plm@mutsaers.com]On Behalf Of Peter
> > Mutsaers
> > Sent: Thursday, July 26, 2001 11:16 PM
> > To: velocity-user@jakarta.apache.org
> > Subject: Re: VTL: How to escape "in a string?
> >
> >
> > >> "Jeremy" == Jeremy Leader <jl...@alumni.caltech.edu> writes:
> >
> >     Jeremy> If the latter (\" becomes " everywhere) how would I put a
> >     Jeremy> single backslash in the middle of schmoo?  Would I have to
> >     Jeremy> double it?  And how would I represent a backslash and a
> >     Jeremy> quote?
> >
> > I'd hope so. Anything else is messy. Everywhere the escape character
> > can be obtained by doubling it.
> >
> > B.t.w., while talking about string literals: How do I get a \n
> > (newline) in a string literal?
> >
> > If I write:
> > #set($strwithnl = "hello this
> > is a string") I get an error. I don't think I can use \n, since the
> > docs say nothing about it. Can I use a \ before the EOL?
> >
> >
> > --
> > Peter Mutsaers  |  Dübendorf    | UNIX - Live free or die
> > plm@gmx.li      |  Switzerland  | Sent via FreeBSD 4.3-stable
> >

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

RE: VTL: How to escape "in a string?

Posted by Tal Dayan <ta...@zapta.com>.
We plan to implement a report generator with Velocity and it will generate
among other things MS Word files in RTF format. RTF format uses backslash
(\) characters intensively (similar to '/' in HTML/XML).

Will we have to escape each of them or does a something like {\xyz... in the
shmoooo
will be preserved as is ? What about the proposed escaping feature, will it
change it ?

Thanks,

Tal



> -----Original Message-----
> From: plm@mutsaers.com [mailto:plm@mutsaers.com]On Behalf Of Peter
> Mutsaers
> Sent: Thursday, July 26, 2001 11:16 PM
> To: velocity-user@jakarta.apache.org
> Subject: Re: VTL: How to escape "in a string?
>
>
> >> "Jeremy" == Jeremy Leader <jl...@alumni.caltech.edu> writes:
>
>     Jeremy> If the latter (\" becomes " everywhere) how would I put a
>     Jeremy> single backslash in the middle of schmoo?  Would I have to
>     Jeremy> double it?  And how would I represent a backslash and a
>     Jeremy> quote?
>
> I'd hope so. Anything else is messy. Everywhere the escape character
> can be obtained by doubling it.
>
> B.t.w., while talking about string literals: How do I get a \n
> (newline) in a string literal?
>
> If I write:
> #set($strwithnl = "hello this
> is a string") I get an error. I don't think I can use \n, since the
> docs say nothing about it. Can I use a \ before the EOL?
>
>
> --
> Peter Mutsaers  |  Dübendorf    | UNIX - Live free or die
> plm@gmx.li      |  Switzerland  | Sent via FreeBSD 4.3-stable
>


Re: VTL: How to escape "in a string?

Posted by Peter Mutsaers <pe...@mutsaers.com>.
>> "Jeremy" == Jeremy Leader <jl...@alumni.caltech.edu> writes:

    Jeremy> If the latter (\" becomes " everywhere) how would I put a
    Jeremy> single backslash in the middle of schmoo?  Would I have to
    Jeremy> double it?  And how would I represent a backslash and a
    Jeremy> quote?

I'd hope so. Anything else is messy. Everywhere the escape character
can be obtained by doubling it.

B.t.w., while talking about string literals: How do I get a \n
(newline) in a string literal?

If I write:
#set($strwithnl = "hello this
is a string") I get an error. I don't think I can use \n, since the
docs say nothing about it. Can I use a \ before the EOL?


-- 
Peter Mutsaers  |  Dübendorf    | UNIX - Live free or die
plm@gmx.li      |  Switzerland  | Sent via FreeBSD 4.3-stable

Re: VTL: How to escape "in a string?

Posted by Jeremy Leader <jl...@alumni.caltech.edu>.
At 03:29 PM 7/26/01 , Geir Magnusson Jr. wrote:
>Peter Mutsaers wrote:
> > 
> > Trying the obivous, using \ to escape the string (\"date\") somehow
> > works (rather mysteriously) but cases the output to have \"date\"
> > literally, that is, the escape character is not removed.
> > 
> > Is this a bug?
>
>:)
>
>No, it's an 'incomplete'.  I added the support for \" in the parser, but
>never added the code to support it, wanting to see what the community
>wanted.  
>
>I think that the general sentiment would be to put the last bit in, eh?
>
>geir

I'd love to have this (in fact I pulled my hair out wrestling
with this in hrefs when I first started with Velocity), but I
have a few questions about the details of how it would work.

Would \" turn into " only in "values", that is things being
manipulated by Velocity, like inside directives and in
macro parameters?  Or would the translation occur even in
schmoo?

If the latter (\" becomes " everywhere) how would I put a single
backslash in the middle of schmoo?  Would I have to double it?
And how would I represent a backslash and a quote?

Jeremy


Re: VTL: How to escape "in a string?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Peter Mutsaers wrote:
> 
> Trying the obivous, using \ to escape the string (\"date\") somehow
> works (rather mysteriously) but cases the output to have \"date\"
> literally, that is, the escape character is not removed.
> 
> Is this a bug?

:)

No, it's an 'incomplete'.  I added the support for \" in the parser, but
never added the code to support it, wanting to see what the community
wanted.  

I think that the general sentiment would be to put the last bit in, eh?

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.