You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Will Hartung <re...@sbcglobal.net> on 2007/07/29 02:54:39 UTC

Escaping Velocity

Hi all,

I'm having issues escaping the Velocity-like parts of a script properly.

I am trying to generate JSP files, and with the use of JSTL and JSP  
EL, a JSP looks a LOT like a Velocity script.

Here's the original text I wanted to escape, in JSP syntax:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>

<t:story category="<<<id>>>" id="${param.id}"/>

<t:page title="<<<title>>>" section="<<<section>>>">
     ${story.properties['html'].value}
</t:page>

The <<<>>> delimited bits are what I want to replace with Velocity.

I tried simply:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>

<t:story category="$category" id="\${param.id}"/>

<t:page title="$title" section="$section">
     \${story.properties['html'].value}
</t:page>

But Velocity complained about the ['html'] section, giving me a  
syntax error.

So, I tried:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>

<t:story category="$page.properties.category.intValue" id="\$ 
{param.id}"/>

<t:page title="$page.title" section="$page.properties.section">
     \$\{story.properties['html'].value\}
</t:page>

But when I render the script, I get all of the \ in the script  
output. It looks like this:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="t" tagdir="/WEB-INF/tags"%>

<t:story category="46051" id="\${param.id}"/>

<t:page title="Test Page" section="testsection">
     \$\{story.properties['html'].value\}
</t:page>

So, I'm kind of stuck. I thought I was escaping things properly, but  
apparently not as I'm not getting out what I expect.

Is there a routine somewhere that will take raw text and convert it  
in to a valid Velocity script the recreates the original text? That  
would be a great help.

I saw the EscapeTool, but it handles a bunch of languages, but not  
Velocity itself.

Does anyone have any examples?

Any help would be appreciated.

Regards,

Will Hartung


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


Re: Escaping Velocity

Posted by Christoph Reck <ap...@recks.org>.
I suggest to use the "poor-mans-escaping" that always works:

#set( $D = '$' )
...
<t:story category="$page.properties.category.intValue" id="${D}{param.id}"/>
<t:page title="$page.title" section="$page.properties.section">
     ${D}{story.properties['html'].value}
</t:page>
...

Hope this helps,
Christoph


Will Hartung wrote:
> Hi all,
> 
> I'm having issues escaping the Velocity-like parts of a script properly.
> 
> I am trying to generate JSP files, and with the use of JSTL and JSP EL, 
> a JSP looks a LOT like a Velocity script.
> 
> Here's the original text I wanted to escape, in JSP syntax:
> 
> <%@page contentType="text/html"%>
> <%@page pageEncoding="UTF-8"%>
> <%@taglib prefix="t" tagdir="/WEB-INF/tags"%>
> 
> <t:story category="<<<id>>>" id="${param.id}"/>
> 
> <t:page title="<<<title>>>" section="<<<section>>>">
>     ${story.properties['html'].value}
> </t:page>
> 
> The <<<>>> delimited bits are what I want to replace with Velocity.
> 
> I tried simply:
> 
> <%@page contentType="text/html"%>
> <%@page pageEncoding="UTF-8"%>
> <%@taglib prefix="t" tagdir="/WEB-INF/tags"%>
> 
> <t:story category="$category" id="\${param.id}"/>
> 
> <t:page title="$title" section="$section">
>     \${story.properties['html'].value}
> </t:page>
> 
> But Velocity complained about the ['html'] section, giving me a syntax 
> error.
> 
> So, I tried:
> 
> <%@page contentType="text/html"%>
> <%@page pageEncoding="UTF-8"%>
> <%@taglib prefix="t" tagdir="/WEB-INF/tags"%>
> 
> <t:story category="$page.properties.category.intValue" id="\${param.id}"/>
> 
> <t:page title="$page.title" section="$page.properties.section">
>     \$\{story.properties['html'].value\}
> </t:page>
> 
> But when I render the script, I get all of the \ in the script output. 
> It looks like this:
> 
> <%@page contentType="text/html"%>
> <%@page pageEncoding="UTF-8"%>
> <%@taglib prefix="t" tagdir="/WEB-INF/tags"%>
> 
> <t:story category="46051" id="\${param.id}"/>
> 
> <t:page title="Test Page" section="testsection">
>     \$\{story.properties['html'].value\}
> </t:page>
> 
> So, I'm kind of stuck. I thought I was escaping things properly, but 
> apparently not as I'm not getting out what I expect.
> 
> Is there a routine somewhere that will take raw text and convert it in 
> to a valid Velocity script the recreates the original text? That would 
> be a great help.
> 
> I saw the EscapeTool, but it handles a bunch of languages, but not 
> Velocity itself.
> 
> Does anyone have any examples?
> 
> Any help would be appreciated.
> 
> Regards,
> 
> Will Hartung
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 


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