You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by John McNally <jm...@collab.net> on 2001/07/25 03:36:25 UTC

variables retaining value in #macro

if I have a macro

#macro (example $foo)
  #set ($tempString = $foo.Name)
  ...
#end

And I have a template  

...
#set ($tempString = $bankAccount.PIN)
#example($customer)
...

Then $tempString inside the macro will retain the value it in the
calling template, if $customer.Name returns null.

Is this considered a useful feature under some conditions?  If not,
would it not be better if a variable inside macro did not get
initialized, it had no value?

john mcnally

Re: variables retaining value in #macro

Posted by Christoph Reck <Ch...@dlr.de>.
"Geir Magnusson Jr." wrote:
> 
> John McNally wrote:
> >
> > if I have a macro
> >
> > #macro (example $foo)

The quick solution would be to add a #if( $foo.Name ) here...

> >   #set ($tempString = $foo.Name)
> >   ...
> > #end
> >
> > And I have a template
> >
> > ...
> > #set ($tempString = $bankAccount.PIN)
> > #example($customer)
> > ...
> >
> > Then $tempString inside the macro will retain the value it in the
> > calling template, if $customer.Name returns null.
> >
> > Is this considered a useful feature under some conditions?  If not,
> > would it not be better if a variable inside macro did not get
> > initialized, it had no value?
> 
> This behavior is what you would expect if you took the body of the VM
> and pasted it in place where it is called, which is what VMs are
> supposed to do.
> 
> I guess there are two solutions to get the alternate behavior :
> 
> 1) Use an event cartridge to catch the null set - however, we don't
> allow things in the context to be null-valued (maybe just remove it...),
> so I am not sure what you really could do there

My experience shows that you do allow null values in the context. 
(The context.put("key", null) will emit a log message.)

Geir, I haven't updated my code to use the EventCartrige yet. So I have
used #if( $test.item.for.null ) in my code to avoid that log message.
The reson for this is I'm evaluating JDOM nodes, e.g. 
  #set( $name = $node.getAttribute("name") )
Therefore I do get NULLs back many times, so I need to do now:
  #set( $name = "#if($node.getAttribute("name"))$node.getAttribute("name")#end" )

> 
> 2) Add logic to do that in a VM, but that offers the problem of null
> valued again in the context as well as behavior that diverges from that
> of 'regular' VTL code.

See the #if logic stated above...

> 
> geir

:) Christoph

Re: VelocityEngine

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Dan Bachelder wrote:
> 
> Log4j handles this in interesting ways... you could wrap it up and handle
> logging that way...

We already have a Log4J logger in Velocity :)

 
> > > So maybe the auto-init() is the way to go, with a
> > > log message...
> >
> > sure, but please think about beginner programmers. in
> > school they don't teach logs, it's either System.out,
> > or nothing happened. So maybe auto-init() should log
> > AND print to System.out
> >
> > piero
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Make international calls for as low as $.04/minute with Yahoo! Messenger
> > http://phonecard.yahoo.com/
> >

-- 
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: VelocityEngine

Posted by Dan Bachelder <ch...@chowda.net>.
Log4j handles this in interesting ways... you could wrap it up and handle
logging that way...

> > So maybe the auto-init() is the way to go, with a
> > log message...
>
> sure, but please think about beginner programmers. in
> school they don't teach logs, it's either System.out,
> or nothing happened. So maybe auto-init() should log
> AND print to System.out
>
> piero
>
> __________________________________________________
> Do You Yahoo!?
> Make international calls for as low as $.04/minute with Yahoo! Messenger
> http://phonecard.yahoo.com/
>


RE: VelocityEngine

Posted by Magnus ?or Torfason <ma...@handtolvur.is>.
If you are so advanced that you need to know about the auto-init, you
probably know about logs also.  When you are getting started, you couldn't
care less if everything is inited in the constructor or if there is an
auto-init method later in the process.

Geir, something that has bothered me when I have implemented auto-init stuff
is that the initialization can often throw extra exceptions, that are not
thrown normally.

VelocityEngine ve = new VelocityEngine();
Template t = ve.getTemplate( "foo");

What if the init method throws an InitializationException, it can pass
through over into getTemplate.  Which is bad.

Cheers,

Magnus

> -----Original Message-----
> From: piero de salvia [mailto:piergi@yahoo.com]
> Sent: 25. juli 2001 11:54
> To: velocity-user@jakarta.apache.org
> Subject: Re: VelocityEngine
>
>
> > So maybe the auto-init() is the way to go, with a
> > log message...
>
> sure, but please think about beginner programmers. in
> school they don't teach logs, it's either System.out,
> or nothing happened. So maybe auto-init() should log
> AND print to System.out
>
> piero
>
> __________________________________________________
> Do You Yahoo!?
> Make international calls for as low as $.04/minute with
> Yahoo! Messenger
> http://phonecard.yahoo.com/


Re: VelocityEngine

Posted by piero de salvia <pi...@yahoo.com>.
> So maybe the auto-init() is the way to go, with a
> log message...

sure, but please think about beginner programmers. in
school they don't teach logs, it's either System.out,
or nothing happened. So maybe auto-init() should log
AND print to System.out

piero

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

Re: VelocityEngine

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
piero de salvia wrote:
> 
> for the init() part:
> 
> throwing an Exception all over for a sin of
> initialization seems very user-unfriendly to me. Now,
> given that VelocityEngine is a proper class, can't we
> just do a constructor with the props (String or
> Properties) so one would be obliged to make an
> instance by providing props ?

Hm.  The problem is that you might want to not use a properties or
something else to set paramters, but use the setProperty() method do to
a bunch, in which case you have to follow up with an init().

So we could have a set of CTORS

VelocityEngine( Properties )
VelocityEngine( Configuration )
VelocityEngine( String )

which call init() for you, but then there is an asymmetry in that 

VelocityEngine()

which is there so you can then setProperty(), will require an init(),
which is too wierd of an API for me.

So maybe the auto-init() is the way to go, with a log message...

> 
> Maybe a third version of the constructor could be
> 
> VelocityEngine(.., ..., static) {...}
> 
> and that would go straight to the good old static
> Runtime.

VelocityEngine and the static Runtime will never cross again :)  That
defeats the purpose.

 
> In any case, the only places where I would throw an
> exception for not initializing are :
> 
> VelocityEngine.getTemplate() and
> 
> Template.merge()
> 
> and both would say : please call init()...

With velocityEngine, its either none or all, and Template.merge()
already throws exceptions - it has nothing to do if you get the Template
from VelocityEngine.getTemplate() or Velocity.getTemplate()

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: VelocityEngine

Posted by piero de salvia <pi...@yahoo.com>.
for the init() part:

throwing an Exception all over for a sin of
initialization seems very user-unfriendly to me. Now,
given that VelocityEngine is a proper class, can't we
just do a constructor with the props (String or
Properties) so one would be obliged to make an
instance by providing props ? 

Maybe a third version of the constructor could be 

VelocityEngine(.., ..., static) {...}

and that would go straight to the good old static
Runtime.

In any case, the only places where I would throw an
exception for not initializing are :

VelocityEngine.getTemplate() and

Template.merge()

and both would say : please call init()...

piero 

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

Re: VelocityEngine

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
piero de salvia wrote:
> 
> Hi all,
> 
> I just tested VelocityEngine in my webapp. It works
> fine. I hope I will never do
> 
>         Velocity.init( props );
> 
> again. Plus it's way faster.

You still have to init() :)

Which brings up an issue - init() is critical for proper functioning,
just like it was/is for the singlton usage.

So if you do something like

VelocityEngine ve = new VelocityEngine();

Template t = ve.getTemplate( "foo");

then bad things happen, because it wasn't initialized.  There are two
ways that occurr to me offhand to deal with this:

1) Have every method in VelocityEngine be capable of throwing an
exception (EngineNotInitException) or something like that, which is
good, as you know immediately, and bad, as you have to put a try block
around every call

2) Have an 'auto-init' which just calls init() for you if you don't. 
This is very convenient, but a little too auto-magical....

> 
> I managed to read the tgz source by getting it from
> webcvs as text, but the real problem was my mediocre
> understanding of Java in general and Velocity (ehm,
> ehm...)...I can see Jon...these idiots should learn
> Java before getting in here...

No - you can learn as you go along.  A little java knowledge is required
to use it though.
 
> So PLEASE don't drop it, as it helps tremendously and
> it's only good.
> 
> I will paraphrase a taxi driver from Miami to another
> taxi driver: "Remember, you're driving a Ford, not a
> Chevy.." and I will say:
> "Remember guys, this is Velocity, not Cold Fusion.."

I think this is a candidate for a quotes page.

-- 
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: VelocityEngine

Posted by piero de salvia <pi...@yahoo.com>.
Certainly...just hadn't thought about it.

--- "Geir Magnusson Jr." <ge...@optonline.net> wrote:
> piero de salvia wrote:
> > 
> > OK, I'll ask:
> > 
> > can we have :
> > 
> > Iterator VelocityEngine.getConfigKeys() ?
> 
> Why not just have a getConfiguration() and then do
> whatever you want to
> the Configuration?
> 
> -- 
> 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.


__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

Re: VelocityEngine

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
piero de salvia wrote:
> 
> OK, I'll ask:
> 
> can we have :
> 
> Iterator VelocityEngine.getConfigKeys() ?

Why not just have a getConfiguration() and then do whatever you want to
the Configuration?

-- 
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: VelocityEngine

Posted by piero de salvia <pi...@yahoo.com>.
OK, I'll ask:

can we have :

Iterator VelocityEngine.getConfigKeys() ?

piero 

--- "Geir Magnusson Jr." <ge...@optonline.net> wrote:
> piero de salvia wrote:
> > 
> > A note :
> > 
> > since we cannot do this anymore :
> > 
> > Iterator i =
> >
>
org.apache.velocity.runtime.Runtime.getConfiguration().getKeys();
> > 
> > should we have this :
> > 
> > Iterator i = vEngine.getKeys();
> > 
> > or maybe this:
> > 
> > RuntimeInstance r = vEngine.getRuntimeInstance();
> > 
> 
> or just add the getConfiguration() method to
> VelocityEngine.
> 
> I am trying, as before, to get people away from
> using Runtime,
> RuntimeInstance, and RuntimeSingleton, and have them
> use Velocity and
> VelocityEngine.
> 
> If there is something needed in the latter two, just
> ask.
> 
> 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.


__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

Re: VelocityEngine

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
piero de salvia wrote:
> 
> A note :
> 
> since we cannot do this anymore :
> 
> Iterator i =
> org.apache.velocity.runtime.Runtime.getConfiguration().getKeys();
> 
> should we have this :
> 
> Iterator i = vEngine.getKeys();
> 
> or maybe this:
> 
> RuntimeInstance r = vEngine.getRuntimeInstance();
> 

or just add the getConfiguration() method to VelocityEngine.

I am trying, as before, to get people away from using Runtime,
RuntimeInstance, and RuntimeSingleton, and have them use Velocity and
VelocityEngine.

If there is something needed in the latter two, just ask.

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: VelocityEngine

Posted by piero de salvia <pi...@yahoo.com>.
A note :

since we cannot do this anymore :

Iterator i =
org.apache.velocity.runtime.Runtime.getConfiguration().getKeys();

should we have this :

Iterator i = vEngine.getKeys();

or maybe this:

RuntimeInstance r = vEngine.getRuntimeInstance();

piero de salvia

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

VelocityEngine

Posted by piero de salvia <pi...@yahoo.com>.
Hi all,

I just tested VelocityEngine in my webapp. It works
fine. I hope I will never do 		

	Velocity.init( props );

again. Plus it's way faster.

I managed to read the tgz source by getting it from
webcvs as text, but the real problem was my mediocre
understanding of Java in general and Velocity (ehm,
ehm...)...I can see Jon...these idiots should learn
Java before getting in here...

So PLEASE don't drop it, as it helps tremendously and
it's only good.

I will paraphrase a taxi driver from Miami to another
taxi driver: "Remember, you're driving a Ford, not a
Chevy.." and I will say:
"Remember guys, this is Velocity, not Cold Fusion.."

piero de salvia

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

Re: variables retaining value in #macro

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
John McNally wrote:
> 
> if I have a macro
> 
> #macro (example $foo)
>   #set ($tempString = $foo.Name)
>   ...
> #end
> 
> And I have a template
> 
> ...
> #set ($tempString = $bankAccount.PIN)
> #example($customer)
> ...
> 
> Then $tempString inside the macro will retain the value it in the
> calling template, if $customer.Name returns null.
> 
> Is this considered a useful feature under some conditions?  If not,
> would it not be better if a variable inside macro did not get
> initialized, it had no value?

This behavior is what you would expect if you took the body of the VM
and pasted it in place where it is called, which is what VMs are
supposed to do.

I guess there are two solutions to get the alternate behavior :

1) Use an event cartridge to catch the null set - however, we don't
allow things in the context to be null-valued (maybe just remove it...),
so I am not sure what you really could do there

2) Add logic to do that in a VM, but that offers the problem of null
valued again in the context as well as behavior that diverges from that
of 'regular' VTL code.

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.