You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Ravikanth L <ra...@gmail.com> on 2005/02/28 10:00:55 UTC

Creating objects in template using "new" keyword

Hi All,

 I would like to create a object reference in template using "new"
keyword like what we do in java

String obj = new String()

I need to put RenderTool instance in context and use it to fix some
problem. I know that i can put it to context from the java code where
the template is being called or using toolbox.xml  But as my
application is standalone and in production and i don't want to change
the java code to fix the issue, i want to handle it in template.

And also is there any way to get the reference to the context in
templates when Velocity.mergeTemplate() is being used, though the
context reference is not kept in context.

Is it possible to achieve this ? Any suggestions are most welcome. 

-- 
With Regards,
Ravikanth.L

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


Re: Creating objects in template using "new" keyword

Posted by Ravikanth L <ra...@gmail.com>.
Hi Shinobu,

Thank you very much for the information. It was a simple java code
change only to add two new instances to the context from the java
code, but i wanted to avoid java code change.

-- 
With Regards,
Ravikanth.L

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


Re: Creating objects in template using "new" keyword

Posted by Nathan Bubna <nb...@gmail.com>.
On Wed, 2 Mar 2005 22:33:47 -0800, Shinobu Kawai
<sh...@gmail.com> wrote:
> Hi Ravikanth,
> 
> > I would like to create a object reference in template using "new"
> > keyword like what we do in java
> >
> > String obj = new String()

<cringe>not a best practice!</cringe> :)

> > I need to put RenderTool instance in context and use it to fix some
> > problem. I know that i can put it to context from the java code where
> > the template is being called or using toolbox.xml  But as my
> > application is standalone and in production and i don't want to change
> > the java code to fix the issue, i want to handle it in template.
> 
> If you really, really, really want to do it, check out the deprecated
> ToolLoader.
>   http://jakarta.apache.org/velocity/tools/javadoc/org/apache/velocity/tools/generic/ToolLoader.html

but this is gone in VelocityTools 1.2

> Or, you can call Class#newInstance from anywhere.
> See Will's HackingVelocity power point about how to do this.
>   http://wiki.apache.org/jakarta-velocity/HackingVelocity
> ## Under "An Evil Velocity Template"

if you have to do this, i recommend this way.  just to make sure it's
clear that it's not a good thing to be doing. ;-)

> > And also is there any way to get the reference to the context in
> > templates when Velocity.mergeTemplate() is being used, though the
> > context reference is not kept in context.
> 
> None that I know of.
> 
> > Is it possible to achieve this ? Any suggestions are most welcome.
> 
> What would be the risks of adding the two lines to your java code?  If
> none of the templates already use the references "$render" or "$ctx",
> then I think you can update it without much worry.
> 
> I also think there was a post in bugzilla some while ago about a
> toolbox for standalone apps.
> 
> Best regards,
> -- Shinobu
> 
> --
> Shinobu Kawai <sh...@gmail.com>
> 
> ---------------------------------------------------------------------
> 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: Creating objects in template using "new" keyword

Posted by Nathan Bubna <nb...@gmail.com>.
On Thu, 3 Mar 2005 08:35:50 -0800, Will Glass-Husain <wg...@forio.com> wrote:
> (moving to velocity-dev from velocity-user)
> 
> Just a quick note:
> 
> I'm not sure we should encourage people to call Class.newInstance() or
> getClassLoader() in a velocity template.  I'd like to see this capability go
> away (or at least be prevented by default in the future).  Seems
> counterproductive to encourage users to rely on it.  (although I appreciate
> that Shinobu's note emphasized the alternatives).

agreed. in fact, i'd like to see us not encourage or support the
creation of arbitrary classes within templates *at all.*  that's why
ToolLoader is deprecated and will soon be abolished entirely.  if
people want to provide their own tools to hang themselves, that's
fine.  but i'd rather not see Velocity or VelocityTools helping them
out.

> Best,
> WILL
> 
> ----- Original Message -----
> From: "Shinobu Kawai" <sh...@gmail.com>
> To: "Velocity Users List" <ve...@jakarta.apache.org>
> Sent: Wednesday, March 02, 2005 10:33 PM
> Subject: Re: Creating objects in template using "new" keyword
> 
> > Or, you can call Class#newInstance from anywhere.
> > See Will's HackingVelocity power point about how to do this.
> >  http://wiki.apache.org/jakarta-velocity/HackingVelocity
> > ## Under "An Evil Velocity Template"
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-dev-help@jakarta.apache.org
> 
>

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


Re: Creating objects in template using "new" keyword

Posted by Will Glass-Husain <wg...@forio.com>.
(moving to velocity-dev from velocity-user)

Just a quick note:

I'm not sure we should encourage people to call Class.newInstance() or 
getClassLoader() in a velocity template.  I'd like to see this capability go 
away (or at least be prevented by default in the future).  Seems 
counterproductive to encourage users to rely on it.  (although I appreciate 
that Shinobu's note emphasized the alternatives).

Best,
WILL

----- Original Message ----- 
From: "Shinobu Kawai" <sh...@gmail.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Wednesday, March 02, 2005 10:33 PM
Subject: Re: Creating objects in template using "new" keyword


> Or, you can call Class#newInstance from anywhere.
> See Will's HackingVelocity power point about how to do this.
>  http://wiki.apache.org/jakarta-velocity/HackingVelocity
> ## Under "An Evil Velocity Template"
>


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


Re: Creating objects in template using "new" keyword

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

> I would like to create a object reference in template using "new"
> keyword like what we do in java
> 
> String obj = new String()
> 
> I need to put RenderTool instance in context and use it to fix some
> problem. I know that i can put it to context from the java code where
> the template is being called or using toolbox.xml  But as my
> application is standalone and in production and i don't want to change
> the java code to fix the issue, i want to handle it in template.

If you really, really, really want to do it, check out the deprecated
ToolLoader.
  http://jakarta.apache.org/velocity/tools/javadoc/org/apache/velocity/tools/generic/ToolLoader.html

Or, you can call Class#newInstance from anywhere.
See Will's HackingVelocity power point about how to do this.
  http://wiki.apache.org/jakarta-velocity/HackingVelocity
## Under "An Evil Velocity Template"

> And also is there any way to get the reference to the context in
> templates when Velocity.mergeTemplate() is being used, though the
> context reference is not kept in context.

None that I know of.

> Is it possible to achieve this ? Any suggestions are most welcome.

What would be the risks of adding the two lines to your java code?  If
none of the templates already use the references "$render" or "$ctx",
then I think you can update it without much worry.

I also think there was a post in bugzilla some while ago about a
toolbox for standalone apps.

Best regards,
-- Shinobu

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

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


Re: JarResourceLoader

Posted by Simon Christian <si...@stoutstick.com>.
Hi Stan,

Stan Devyatovsky wrote:
> Hello Shinobu,
> 
> 
>>> If I use JarResourceLoader, will any uploads of new .jar with
>>> templates be notified by it?
>>> I.e., I first uploaded templates.jar, started webapp,
>>> then reuploaded modified templates.jar - will my modifications
>>> take place without restarting webapplication?
> 
> SK> It should.  If you look at the JarResourceLoader implementation,
> SK> you'll notice that isSourceModified is always true.
> In that case, it will always recompile the Velocity templates from
> .JAR file, no matter what? That's not too performance-wise in that
> case.

Not strictly - if caching is on it will only check if the source has 
been 'modified' and therefore recompile according to the interval 
specified in

   xxx.resource.loader.modificationCheckInterval

In a production environment, you'd probably expect to set that to quite 
a high value anyway.

- simon

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


Re[2]: JarResourceLoader

Posted by Stan Devyatovsky <st...@devyatovsky.com>.
Hello Shinobu,

>>  If I use JarResourceLoader, will any uploads of new .jar with
>>  templates be notified by it?
>>  I.e., I first uploaded templates.jar, started webapp,
>>  then reuploaded modified templates.jar - will my modifications
>>  take place without restarting webapplication?
SK> It should.  If you look at the JarResourceLoader implementation,
SK> you'll notice that isSourceModified is always true.
In that case, it will always recompile the Velocity templates from
.JAR file, no matter what? That's not too performance-wise in that
case.

-- 
Best regards,
 Stan                            mailto:stan@devyatovsky.com


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


Re: JarResourceLoader

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

>  If I use JarResourceLoader, will any uploads of new .jar with
>  templates be notified by it?
>  I.e., I first uploaded templates.jar, started webapp,
>  then reuploaded modified templates.jar - will my modifications
>  take place without restarting webapplication?

It should.  If you look at the JarResourceLoader implementation,
you'll notice that isSourceModified is always true.

Best regards,
-- Shinobu

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

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


JarResourceLoader

Posted by Stan Devyatovsky <st...@devyatovsky.com>.
Hello,

  If I use JarResourceLoader, will any uploads of new .jar with
  templates be notified by it?
  I.e., I first uploaded templates.jar, started webapp,
  then reuploaded modified templates.jar - will my modifications
  take place without restarting webapplication?

  Thank you in advance.
  
-- 
Best regards,
 Stan                            mailto:stan@devyatovsky.com


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