velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tomoki Tsuchida" <ttsu...@studioyk.dhs.org>
Subject Re: Question on recursion
Date Wed, 28 Nov 2001 19:44:01 GMT
Thanks for your help, I didn't know about velocimacro.context.localscope
parameter... I put it in my velocity.properties but it didn't make any
change however.  I'll be checking to see if I'm using the correct property
file and everything, but meanwhile, here's a simpler demonstration of the
problem:

[Recursion.vm]
#macro ( recurse $group $parent )
  #foreach ( $stuff in $group.Groups )
  $parent$stuff.Name<br>
  #recurse ( $stuff "$parent$stuff.Name &gt;" )
  #end
#end

<html>
<body>
#recurse ( $mygroup "" )
</body>
</html>

[in java]
  public class Group {
    private String name;
    public List groups = new ArrayList();
    public Group(String name) { this.name = name; }
    public List getGroups() { return this.groups; }
    public String getName() { return this.name; }
  }
..
    Group root = new Group("root");
    Group one = new Group("one");
    Group two = new Group("two");

    Group oneb = new Group("one one");
    Group onec = new Group("one two");
    Group oned = new Group("one three");

    Group onebb = new Group ("one one one");
    oneb.groups.add(onebb);

    one.groups.add(oneb);
    one.groups.add(onec);
    one.groups.add(oned);

    Group twob = new Group("two one");
    Group twoc = new Group("two two");
    two.groups.add(twob);
    two.groups.add(twoc);

    root.groups.add(one);
    root.groups.add(two);

    context.put("mygroup", root);

This will produce...

one
one one >one one
one one one >one one one >one one one
one two >one two
one three >one three
two
two one >two one
two two >two two

I guess another issue is the way velocity resolves variable by name at run
time (right?).  So "$parent$stuff.Name &gt;" gets resolved to "$stuff.Name
&gt; $stuff.Name &gt;" on the 2nd level, which is not what I want.  In the
xmlapp_example it isn't a problem because #recursion is called with just
"$index " (so it gets resolved to "$index $index $index " etc., and $index
starts out empty).  Does the local scope option really make velocity resolve
the variables before it calls another macro?

I'm using velocity 1.2-rc3.

Thanks,
Tomoki

----- Original Message -----
From: "Geir Magnusson Jr." <geirm@optonline.net>
To: <velocity-user@jakarta.apache.org>
Sent: Wednesday, November 28, 2001 11:14 AM
Subject: Re: Question on recursion


> Recursion works fine, but yes, you can step on yourself :)
>
> There is a switch to turn on localscope contexts in VMs.  See the dev
> guide..
>
> Should be something like
>
> velocimacro.context.localscope = true
>
> I'm a little surprised you are having problems - what version of Velocity?
> There is an example, examples/xmlapp_example that demonstrats VM
recursion,
> has the same thing, there it's passing in an $indent string that keeps
> changing...
>
>
> On 11/28/01 10:48 AM, "Tomoki Tsuchida" <ttsuchi@studioyk.dhs.org> wrote:
>
> > Hello,
> >
> > I've been working on velocity for a while now, and now I'm trying to
flatten
> > tree structure in velocity and ran into trouble.  I have two java
classes,
> > 'categorygroup' and 'category', and I want to show all of the categories
> > belonging to category groups in a select list. A category belongs to a
> > categorygroup, and a categorygroup can belong to another categorygroup.
So
> > the classes look like this:
> >
> > public class category {
> > public String getId() {..}
> > public String getName() {..}
> > }
> >
> > public class categorygroup {
> > public String getName() {.. }
> > public List /* of category */ getCategories() {..}
> > public List /* of categorygroup */ getCategorygroups() {..}
> > }
> >
> > .. and when I have a structure like this (brackets are categories):
> >
> > + [root category 1]
> > + [root category 2]
> > + category group 1
> > - [cat under group 1]
> > + category group 2
> >   - [cat under 2]
> >   - [another cat under 2]
> > + category group 3
> >  - [cat under group 3]
> >
> > .. I want to show it like this in the select list:
> >
> > root category 1
> > root category 2
> > category group 1 > cat under group 1
> > category group 1 > category group 2 > cat under 2
> > category group 1 > category group 2 > another cat under 2
> > category group 3 > cat under group 3
> >
> > I had to do this in java before and it's pretty easy with recursive
> > functions.  Now what I want to do is to dump the root categorygroup into
the
> > context object and flatten the tree structure using velocimacro instead
of
> > java.  And I came up with something like this:
> >
> > #macro ( recurse $group $parentname )
> > #foreach ( $category in $group.Category )
> > <option value="$category.Id">$parentname$category.Name
> > #end
> > #foreach ( $childgroup in $group.Categorygroup )
> > #recurse ( $childgroup "$parentname$childgroup.Name &gt; " )
> > #end
> > #end
> >
> > ...
> >
> > <select>
> > #recurse( $mygroup "" )
> > </select>
> >
> > I looked at the XML tree example first and came up with the macro.  The
> > problem is, it doesn't work like the way I want it to.  If you run this
> > macro with the aforementioned data set it will render something like:
> >
> > root category 1
> > root category 2
> > category group 1 > cat under group 1
> > category group 2 > category group 2 > cat under 2
> > category group 2 > category group 2 > another cat under 2
> > category group 2 > category group 2 > category group 3 > another cat
under 2
> > category group 2 > category group 2 > category group 3 > cat under group
3
> >
> > I think the problem is that everything is evaluated on a global scope,
i.e.
> > '$childgroup' and '$parentname' gets overwritten every time the child
> > recursive function is called.  Is there any way in velocity to declare a
> > local-scope variable inside a macro?  I guess I should be able to
rewrite
> > the same logic without using recursion, but I can't really think of how
I'd
> > go about doing that either :-p  I could also do it easily within java,
but
> > then it defeats the purpose of separating presentation from logic so I
> > really don't want to do it.
> >
> > Thanks,
> > Tomoki
> >
> >
> >
> > --
> > To unsubscribe, e-mail:
> > <mailto:velocity-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> > <mailto:velocity-user-help@jakarta.apache.org>
> >
>
> --
> Geir Magnusson Jr.     geirm@optonline.net
> System and Software Consulting
> "Whoever would overthrow the liberty of a nation must begin by subduing
the
> freeness of speech." - Benjamin Franklin
>
>
>
> --
> To unsubscribe, e-mail:
<mailto:velocity-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
<mailto:velocity-user-help@jakarta.apache.org>
>
>


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


Mime
View raw message