You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Humberto Hernandez Torres <hh...@itweb.com.mx> on 2004/01/27 18:47:05 UTC

Using Velocity to generate velocity templates.

I want to generate some velocity templates using velocity. But I am afraid I
am going to get lost on escaping the # and $. My question is. Is there a way
to change the # to @ and the $ to let say %. Are there any other tricks or
ideas I could use?

--
  Humberto

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


Re: Using Velocity to generate velocity templates.

Posted by Cesare Rocchi <ro...@itc.it>.
Humberto Hernandez Torres wrote:

I did something similar. Velocity templates are created from an xml via 
XSL transformations. It is xsl stylesheet to add velocity statements to 
the file and then, via merging/Velocity, I generate my result. Obviously 
this implies that you have to design the starting point (the xml) and 
trasformation rules to insert appropriately velocity statements.

Hope this helps,

-c.

> I want to generate some velocity templates using velocity. But I am afraid I
> am going to get lost on escaping the # and $. My question is. Is there a way
> to change the # to @ and the $ to let say %. Are there any other tricks or
> ideas I could use?
> 
> --
>   Humberto
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> 
> 


-- 
+---------------------------------------+
              Cesare Rocchi
  ITC-IRST Povo I-38050 (TRENTO) ITALY
  http://tcc.itc.it/people/rocchi.html


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


Re: Using Velocity to generate velocity templates.

Posted by Barbara Baughman <ba...@utdallas.edu>.
Ooops!  Sorry, I misunderstood the question.  I think I would put the
part of the velocity template that you want to render without parsing
into a different file and use #include("file") to bring that part in
without parsing it.  It may get pretty complicated, but that's one way
to do it.  You may also need to do some escaping.

So your template might look like
#include("beginning")
#foreach ($page in $pages)
  #include("middle")
#end
#if ($somemore)
  #include("somemore.vm")
#end
#include("end")

Where beginning, middle, somemore and end are files thatmay have
Velocity template code.  Note that the #include directive does not
parse the code. Beyond that, escaping is your only tool.

Barbara Baughman
X2157

On Tue, 27 Jan 2004, Barbara Baughman wrote:

> The only time the Velocity pays attention to # is if it is the first
> character of a Velocity directive (#set, #parse, #if, #else, #end,
> #foreach, #include, etc).  The only time it pays attention to $ is if
> it refers to a reference in the Velocity context.  So, for example,
> Velocity would only pay attention to the $ in $20 if the object
> name "20" were in your Velocity context.  So, unless # is part of a
> directive, Velocity renders it as #.  Unless $ is a reference to an
> object name in the Velocity context, it is rendered as $.
>
> In other words, it's not a big problem.
>
> Barbara Baughman
> X2157
>
> On Tue, 27 Jan 2004, Humberto Hernandez Torres wrote:
>
> > I want to generate some velocity templates using velocity. But I am afraid I
> > am going to get lost on escaping the # and $. My question is. Is there a way
> > to change the # to @ and the $ to let say %. Are there any other tricks or
> > ideas I could use?
> >
> > --
> >   Humberto
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: velocity-user-help@jakarta.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>

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


Re: Using Velocity to generate velocity templates.

Posted by Barbara Baughman <ba...@utdallas.edu>.
The only time the Velocity pays attention to # is if it is the first
character of a Velocity directive (#set, #parse, #if, #else, #end,
#foreach, #include, etc).  The only time it pays attention to $ is if
it refers to a reference in the Velocity context.  So, for example,
Velocity would only pay attention to the $ in $20 if the object
name "20" were in your Velocity context.  So, unless # is part of a
directive, Velocity renders it as #.  Unless $ is a reference to an
object name in the Velocity context, it is rendered as $.

In other words, it's not a big problem.

Barbara Baughman
X2157

On Tue, 27 Jan 2004, Humberto Hernandez Torres wrote:

> I want to generate some velocity templates using velocity. But I am afraid I
> am going to get lost on escaping the # and $. My question is. Is there a way
> to change the # to @ and the $ to let say %. Are there any other tricks or
> ideas I could use?
>
> --
>   Humberto
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>

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


Re: Using Velocity to generate velocity templates.

Posted by Mike Kienenberger <mk...@alaska.net>.
Humberto Hernandez Torres <hh...@itweb.com.mx> wrote:
> I want to generate some velocity templates using velocity. But I am afraid 
I
> am going to get lost on escaping the # and $. My question is. Is there a 
way
> to change the # to @ and the $ to let say %. Are there any other tricks or
> ideas I could use?

Unfortunately, not yet.

Your only choices are to use another templating system like FreeMarker? 
FreeMaker?  to create velocity templates (but having to maintain two 
template systems seems like wasted effort) or struggle through it.

The way I've been approaching it is to do this:

Design a "literal view" of what you hope the final template will look like.

Substitute all $s with ${dollarSign} and all # signs with ${poundSign}

Stick these at the top of the template:

#set( $dollarSign = '$' )
#set( $poundSign = '#' )


Add in your control logic and directives.


One of the future improvements to velocity discussed on the dev list is to 
eventually allow directive enclosures.

Ie, {#if}(condition)true-text{#else}false-text{#end}

I think once that sort of parsing is supported in the parser, it'd be easier 
to change the parser to allow the ability to change the grouping symbol to 
something user-defined. 


I guess one other possibility is to choose symbols you know will never be 
used in either template, and do a straight substitution on them with some 
kind of simple preprocessor.

	replace $ with ${dollarSign}
	replace # with ${poundSign}
	replace @ with #
	replace % with $

The trick being to find two recognizable symbols you'd never use.

-Mike

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