You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Aaron Hamid <ar...@cornell.edu> on 2002/11/20 21:48:21 UTC

Velocity macros for recursive display of tree?

Can anybody tell me if Velocity would be given to displaying recursive 
structures?  From a brief skimming over the macro language, it appears 
this would be possible by having a macro call itself.

The problem I have is that I would like to display a tree structure 
(folders, files), but the web (or at least JSP) is a notoriously bad 
medium for something like this.  My first cut with pure JSP was pretty 
painful.  Just recently I wrote a generic custom tag to render trees, 
based on the javax.swing.tree package interfaces (TreeModel, TreeNode), 
but still, while being somewhat less painful, it is pretty ugly 
(basically the recursive descent logic is now replaced by the tag (which 
actually forces you to be iterative instead of recursive), but the 
display is still using embedded JSP).

The logic is something like:

<ul>
render: $node

if $node is a folder
   <li>display $node folder details
   if $node has children
     <ul>
       for each $node child
         render($child)
       end
     </ul>
   fi
   </li>
else
   display <li>$node file details</li>
fi
</ul>

of course there is a bit of logic in the display of the node, so I'd 
have to call out to methods on other classes.

TIA
Aaron


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


Re: Velocity macros for recursive display of tree?

Posted by Claude Brisson <cl...@savoirweb.com>.
it's a common question.

>   What is the problem? You can just write a macro calling itself and
> implement the abstract code you provided.

yes, macros can be recursive, but there might be a problem : macros are inline, so the recursive call destroys macro's local
variables if any (which is not always disturbing, note).

To avoid this, you can use the #local directive, which is not (yet ?) included in the standard package but that you can get from the
cvs repository (http://cvs.apache.org/viewcvs/jakarta-velocity/whiteboard/geir/Local.java : in velocity.jar, put it in
/org/apache/velocity/runtime/directive and then edit the file org/apache/velocity/runtime/defaults/directive.properties).

Use #local as follow :

#local($foo)
... code where $foo explicitely has a local scope
#end

Hope this may help.

CloD

----- Original Message -----
From: "Peter Romianowski" <me...@gmx.de>
To: "'Velocity Users List'" <ve...@jakarta.apache.org>
Sent: mercredi 20 novembre 2002 22:30
Subject: RE: Velocity macros for recursive display of tree?


>   What is the problem? You can just write a macro calling itself and
> implement the abstract code you provided. Could you please clarify
> your question, I didn't get it.
>
> Peter
>
> > -----Original Message-----
> > From: Aaron Hamid [mailto:arh14@cornell.edu]
> > Sent: Wednesday, November 20, 2002 9:48 PM
> > To: velocity-user@jakarta.apache.org
> > Subject: Velocity macros for recursive display of tree?
> >
> >
> > Can anybody tell me if Velocity would be given to displaying
> > recursive
> > structures?  From a brief skimming over the macro language,
> > it appears
> > this would be possible by having a macro call itself.
> >
> > The problem I have is that I would like to display a tree structure
> > (folders, files), but the web (or at least JSP) is a notoriously bad
> > medium for something like this.  My first cut with pure JSP
> > was pretty
> > painful.  Just recently I wrote a generic custom tag to render trees,
> > based on the javax.swing.tree package interfaces (TreeModel,
> > TreeNode),
> > but still, while being somewhat less painful, it is pretty ugly
> > (basically the recursive descent logic is now replaced by the
> > tag (which
> > actually forces you to be iterative instead of recursive), but the
> > display is still using embedded JSP).
> >
> > The logic is something like:
> >
> > <ul>
> > render: $node
> >
> > if $node is a folder
> >    <li>display $node folder details
> >    if $node has children
> >      <ul>
> >        for each $node child
> >          render($child)
> >        end
> >      </ul>
> >    fi
> >    </li>
> > else
> >    display <li>$node file details</li>
> > fi
> > </ul>
> >
> > of course there is a bit of logic in the display of the node, so I'd
> > have to call out to methods on other classes.
> >
> > TIA
> > Aaron
> >
> >
> > --
> > To unsubscribe, e-mail:
> > <mailto:velocity-user-> unsubscribe@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>
>
>


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


RE: Velocity macros for recursive display of tree?

Posted by Peter Romianowski <me...@gmx.de>.
  What is the problem? You can just write a macro calling itself and
implement the abstract code you provided. Could you please clarify
your question, I didn't get it.

Peter

> -----Original Message-----
> From: Aaron Hamid [mailto:arh14@cornell.edu] 
> Sent: Wednesday, November 20, 2002 9:48 PM
> To: velocity-user@jakarta.apache.org
> Subject: Velocity macros for recursive display of tree?
> 
> 
> Can anybody tell me if Velocity would be given to displaying 
> recursive 
> structures?  From a brief skimming over the macro language, 
> it appears 
> this would be possible by having a macro call itself.
> 
> The problem I have is that I would like to display a tree structure 
> (folders, files), but the web (or at least JSP) is a notoriously bad 
> medium for something like this.  My first cut with pure JSP 
> was pretty 
> painful.  Just recently I wrote a generic custom tag to render trees, 
> based on the javax.swing.tree package interfaces (TreeModel, 
> TreeNode), 
> but still, while being somewhat less painful, it is pretty ugly 
> (basically the recursive descent logic is now replaced by the 
> tag (which 
> actually forces you to be iterative instead of recursive), but the 
> display is still using embedded JSP).
> 
> The logic is something like:
> 
> <ul>
> render: $node
> 
> if $node is a folder
>    <li>display $node folder details
>    if $node has children
>      <ul>
>        for each $node child
>          render($child)
>        end
>      </ul>
>    fi
>    </li>
> else
>    display <li>$node file details</li>
> fi
> </ul>
> 
> of course there is a bit of logic in the display of the node, so I'd 
> have to call out to methods on other classes.
> 
> TIA
> Aaron
> 
> 
> --
> To unsubscribe, e-mail:   
> <mailto:velocity-user-> unsubscribe@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>