You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by sandesh makam <sa...@gmail.com> on 2006/09/29 16:27:42 UTC

help in calling a class in a velocity template

hi,
  Can any body tell me how i can call a class in a velocity template.
 I am trying to write a template which call a class that allows me to build
a tree.

thankyou

Re: help in calling a class in a velocity template

Posted by "Henning P. Schmiedehausen" <hp...@intermeta.de>.
"Will Glass-Husain" <wg...@forio.com> writes:

>is just not a good way of doing this.  It's error-prone, hard to read,
>may not be supported in future versions of Velocity.  Instead, create

... will not be supported ...

One of the strengths of Velocity is that we allow the programmer
control on what is available on the templates.

(Gee, leaking that out on the "Hacking Velocity" talk really made that
trick popular... :-) )

	Best regards
		Henning

-- 
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH
hps@intermeta.de        +49 9131 50 654 0   http://www.intermeta.de/

RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for hire
   Linux, Java, perl, Solaris -- Consulting, Training, Development

Social behaviour: Bavarians can be extremely egalitarian and folksy.
                                    -- http://en.wikipedia.org/wiki/Bavaria
Most Franconians do not like to be called Bavarians.
                                    -- http://en.wikipedia.org/wiki/Franconia

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


Re: help in calling a class in a velocity template

Posted by Will Glass-Husain <wg...@forio.com>.
Noooooo...

#set($newobject = $foo.class.forName("myclass").newInstance())

is just not a good way of doing this.  It's error-prone, hard to read,
may not be supported in future versions of Velocity.  Instead, create
an instance of the object and put it into the context before Velocity
is called with
  context.put("newobject", new MyClass());

Note to Claude: I've got a new (simpler) Uberspector subclass that
addresses this.  I'm going to integrate it into 1.5 during our
Hackathon in two weeks.

WILL

On 9/29/06, Claude Brisson <cl...@renegat.net> wrote:
> Do you mean :
>
> "How to put a new instance of a class in the velocity context from
> inside the vtl?"
>
> Usually, you put classes in the context using java
> (context.put("name",object)), or using toolbox.xml if you rely on the
> VelocityTools subproject.
>
> Now, if you really want to load a new instance of a class in the context
> from inside the VTL, you have the following hack:
>
> #set($newobject = $foo.class.forName("myclass").newInstance())
>
> where $foo can be any existing reference.
>
> But this is really not a very clean coding style... Also, the class you
> load must have a default constructor (otherwise it is still possible but
> more complex).
>
> By the way, dev people, why is this issue only scheduled for 1.6? Will's
> patch seems to be ready in
> http://issues.apache.org/jira/browse/VELOCITY-179 and backward
> compatible with proper default configuration value.
>
>   Claude
>
>
> Le vendredi 29 septembre 2006 à 10:27 -0400, sandesh makam a écrit :
> > hi,
> >   Can any body tell me how i can call a class in a velocity template.
> >  I am trying to write a template which call a class that allows me to build
> > a tree.
> >
> > thankyou
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>


-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

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


Re: help in calling a class in a velocity template

Posted by Claude Brisson <cl...@renegat.net>.
Do you mean :

"How to put a new instance of a class in the velocity context from
inside the vtl?"

Usually, you put classes in the context using java
(context.put("name",object)), or using toolbox.xml if you rely on the
VelocityTools subproject.

Now, if you really want to load a new instance of a class in the context
from inside the VTL, you have the following hack:

#set($newobject = $foo.class.forName("myclass").newInstance())

where $foo can be any existing reference.

But this is really not a very clean coding style... Also, the class you
load must have a default constructor (otherwise it is still possible but
more complex).

By the way, dev people, why is this issue only scheduled for 1.6? Will's
patch seems to be ready in
http://issues.apache.org/jira/browse/VELOCITY-179 and backward
compatible with proper default configuration value.

  Claude


Le vendredi 29 septembre 2006 à 10:27 -0400, sandesh makam a écrit :
> hi,
>   Can any body tell me how i can call a class in a velocity template.
>  I am trying to write a template which call a class that allows me to build
> a tree.
> 
> thankyou


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