You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Leos Literak <li...@centrum.cz> on 2002/03/04 11:46:32 UTC

scope of introduced variables

Hello,

I'd like to know, if you are planning any changes
of scope of variables introduced in template.

I've got following problems:

1\
($b=='str', $c=null)

#set($a=$b)
$a
#set($a=$c)
$a

result: str str

I have to be careful about variable names.
I shall not use them again, because they
are not zeroed, when setting to null variable.

I'd prefer to unset variable, if right side
is null. If for any reason you don't want this,
please create #unset

2\ velocimacro

#macro (showArticle $relation $last)

#set($clanek=$relation.Child)
#set($dizs=$UTIL.filterDiscussions($clanek.Content))

#if($dizs.size()>0)
#set($diz=$dizs.get(0).Child)
#end

$!UTIL.showDate($!UTIL.getXPath($clanek,'/data/published'))
$!UTIL.getXPath($clanek,'/data/perex')

Comments:
#if($diz)
$diz.Content.size()
#else
0
#end

#end

When $diz or $dizs are initialized for first time, $diz is always
used in following invocations, until they are reset by new value.
The result is of course wrong.

Yes, I know, there is configuration key for macros local scope.
But I can't use it, because few of my macros forwards data to next
macro in chain:

#macro(first $b)
#set($c=$b.Data)
$b.OtherData: #second($c)
#end

and when I set local scope to true, such macros stopped working.

Again, it could be fixed with explicit #unset, but I'd prefer
to have new tag for local scope variables.
Something like

#macro(my $a)
#setTemp($count=$a.Content.size())
$a, $count
#end

What do you think? I'm tired of making workarounds and hacks to these
limitations. I think, Velocity is really great product and I like it.
But sometimes it is too simple :-(


I will try to watch this thread over HTTP, if I found web interface
to this archive. If you want to ensure, I will see your comments,
cc my email, 'cause I'm not in this conference.

	Leos


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: scope of introduced variables

Posted by Leos Literak <li...@centrum.cz>.
Geir,

> This is a subject of recent discussion.   We are going to fix soon.

yes, I've seen that discussion. It'll be great, when
null value will unset variable. Btw could you also
add token null, so we can do it manually?
#set($tmp=null)


>>#macro(first $b)
>>#set($c=$b.Data)
>>$b.OtherData: #second($c)
>>#end
>>and when I set local scope to true, such macros stopped working.

#second. it is not found, because $c is local
=> it disappears when leaving context of #first

I've tried it only once and it stopped working,
so I immediatelly turned it on

> What stops working?  Passing data from 'first' to 'second'?

I don't undestand ..
> In the first case, why can't you initialize the references to be sure?

agree
> context a reference...  I personally don't like #unset() if we can solve
> with a more useful technique like a 'NULL' (so we can compare to it as


	Leo



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ContextTool

Posted by Jonathan Carlson <jo...@yahoo.com>.
Another MVC-2 implementation that I am very pleased with is 
ActionServlet.  It is documented well, has lots of example code, 
supports "pull" tools (called Components), and even allows you to 
specify/change the flow of control in an XML descriptor file.  It does 
what it does very well without trying to include "everything but the 
kitchen sink".

http://dione.zcu.cz/~toman40/ActionServlet/

Jonathan

-- 
Jonathan Carlson
joncrlsn@users.sf.net
Minneapolis, Minnesota



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ContextTool

Posted by Bill Burton <bi...@progress.com>.
Hello,

Sounds like what you're looking for is some kind of "pull" tool.  The
Turbine framework supports this methodology which you can read about at
http://jakarta.apache.org/turbine/turbine-2/pullmodel.html.  You could
also roll your own pull tool to do something similar.  Create a class with
a "get" method that takes the name of the class (or an alias) that you
want to load.  When the get method in your tool class is called, it would
instantiate the desired class if that had not already been done for the
request and then return the reference to it.   Assuming the tool helper
class was put into the context as "tool", you'd access your class like:
    $tool.myclass...

You could associate the name "myclass" to be used as the context name with
the class name in velocity.properties or by other similar means.

The WebWork framework (http://sourceforge.net/projects/webwork/) supports
a similar but Model-1 type approach.  The #bean macro can be used to
instantiate any class with a public null contstructor. Or, the #action
macro will instantiate a class that implements the WebWork Action
interface.  In both cases, you may need to call the #execute macro to
execute some logic.  WebWork isn't quite released as 1.0, so if you're
interested it would be best to pull it from CVS.

Struts, Turbine, WebWork and other frameworks all support the Model-2
(MVC) approach where requests are directed to a controller servlet that
delegates to some Action class to execute some business logic.  From there
the request is usually forwarded to the template.  However, these Action
type classes tend to be used for business logic as opposed to simple
helper functions.  

-Bill

Shawn Church wrote:
> 
> That's essentially what I'm looking for, as long as the tool is instantiated
> only on its first use.  I have read over the Velocity developer's guide, but
> I may not have noticed the implementation.  So far, I have been explicitly
> loading these tool classes into the context on each request.  However, not
> every page will necessarily make use of these tools, so the instantiation is
> wasted in these cases.
> 
> -----Original Message-----
> From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
> Sent: Monday, March 04, 2002 12:07 PM
> To: velocity-user@jakarta.apache.org
> Subject: Re: ContextTool
> 
> On 3/4/02 12:35 PM, "Shawn Church" <sh...@boxity.com> wrote:
> 
> > I have used WebMacro extensively over the past couple of years, but I am
> > also curious about Velocity.  It seems to work pretty much as expected,
> but
> > I haven't noticed an equivalent interface in Velocity to the WebMacro
> > ContextTool.  Does anything like this exist in Velocity?
> >
> 
> Do you mean autoloading of tool classes in the context?
> 
> --
> Geir Magnusson Jr.                                     geirm@optonline.net
> System and Software Consulting
> 
> Age and treachery will always triumph over youth and talent
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: ContextTool

Posted by Shawn Church <sh...@boxity.com>.
That's essentially what I'm looking for, as long as the tool is instantiated
only on its first use.  I have read over the Velocity developer's guide, but
I may not have noticed the implementation.  So far, I have been explicitly
loading these tool classes into the context on each request.  However, not
every page will necessarily make use of these tools, so the instantiation is
wasted in these cases.

-----Original Message-----
From: Geir Magnusson Jr. [mailto:geirm@optonline.net]
Sent: Monday, March 04, 2002 12:07 PM
To: velocity-user@jakarta.apache.org
Subject: Re: ContextTool


On 3/4/02 12:35 PM, "Shawn Church" <sh...@boxity.com> wrote:

> I have used WebMacro extensively over the past couple of years, but I am
> also curious about Velocity.  It seems to work pretty much as expected,
but
> I haven't noticed an equivalent interface in Velocity to the WebMacro
> ContextTool.  Does anything like this exist in Velocity?
>

Do you mean autoloading of tool classes in the context?

--
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting

Age and treachery will always triumph over youth and talent


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ContextTool

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 3/4/02 12:35 PM, "Shawn Church" <sh...@boxity.com> wrote:

> I have used WebMacro extensively over the past couple of years, but I am
> also curious about Velocity.  It seems to work pretty much as expected, but
> I haven't noticed an equivalent interface in Velocity to the WebMacro
> ContextTool.  Does anything like this exist in Velocity?
> 

Do you mean autoloading of tool classes in the context?

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting

Age and treachery will always triumph over youth and talent


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


ContextTool

Posted by Shawn Church <sh...@boxity.com>.
I have used WebMacro extensively over the past couple of years, but I am
also curious about Velocity.  It seems to work pretty much as expected, but
I haven't noticed an equivalent interface in Velocity to the WebMacro
ContextTool.  Does anything like this exist in Velocity?


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: scope of introduced variables

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 3/4/02 5:46 AM, "Leos Literak" <li...@centrum.cz> wrote:

> Hello,
> 
> I'd like to know, if you are planning any changes
> of scope of variables introduced in template.
> 
> I've got following problems:
> 
> 1\
> ($b=='str', $c=null)
> 
> #set($a=$b)
> $a
> #set($a=$c)
> $a
> 
> result: str str
> 
> I have to be careful about variable names.
> I shall not use them again, because they
> are not zeroed, when setting to null variable.
> 
> I'd prefer to unset variable, if right side
> is null. If for any reason you don't want this,
> please create #unset
> 

This is a subject of recent discussion.   We are going to fix soon.


> 2\ velocimacro
> 
> #macro (showArticle $relation $last)
> 
> #set($clanek=$relation.Child)
> #set($dizs=$UTIL.filterDiscussions($clanek.Content))
> 
> #if($dizs.size()>0)
> #set($diz=$dizs.get(0).Child)
> #end
> 
> $!UTIL.showDate($!UTIL.getXPath($clanek,'/data/published'))
> $!UTIL.getXPath($clanek,'/data/perex')
> 
> Comments:
> #if($diz)
> $diz.Content.size()
> #else
> 0
> #end
> 
> #end
> 
> When $diz or $dizs are initialized for first time, $diz is always
> used in following invocations, until they are reset by new value.
> The result is of course wrong.
> 
> Yes, I know, there is configuration key for macros local scope.
> But I can't use it, because few of my macros forwards data to next
> macro in chain:
> 
> #macro(first $b)
> #set($c=$b.Data)
> $b.OtherData: #second($c)
> #end
> 
> and when I set local scope to true, such macros stopped working.

What stops working?  Passing data from 'first' to 'second'?

In the first case, why can't you initialize the references to be sure?
 
> Again, it could be fixed with explicit #unset, but I'd prefer
> to have new tag for local scope variables.
> Something like
> 
> #macro(my $a)
> #setTemp($count=$a.Content.size())
> $a, $count
> #end
> 
> What do you think? I'm tired of making workarounds and hacks to these
> limitations. I think, Velocity is really great product and I like it.
> But sometimes it is too simple :-(

I think the solution will be a way to unset or clearly remove from the
context a reference...  I personally don't like #unset() if we can solve
with a more useful technique like a 'NULL' (so we can compare to it as
well...)

> 
> I will try to watch this thread over HTTP, if I found web interface
> to this archive. If you want to ensure, I will see your comments,
> cc my email, 'cause I'm not in this conference.
> 

I will cc you once with this, but out of courtesy, if you want to be sure to
get responses, please subscribe.

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"He who throws mud only loses ground." - Fat Albert


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>