Hi Gary, So far as i know, you are in fairly unique territory. The #if and #foreach directives were never really meant to be extended or replaced, so far as i know. Their roots go deep into the parser and will almost certainly require a custom build of Velocity, not just some configuration and extensions. You may be the first to try something much like this. But brave experiments usually teach a lot, so i'm certainly curious to see how it works out and hear if there are ways we can make things easier for such efforts down the road.
In the meantime, i do have a somewhat lateral suggestion: have you considered using a "Tool" type of syntax instead? I know it probably wouldn't be as clean, but it might be a lot easier to implement. I've actually been heading down a parallel road myself lately with the work i've been doing on the LoopTool (http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/LoopTool.java?view=markup) Rather than mess with the parser and core directives, i've made it relatively simple to have a #foreach watched by the LoopTool to automatically skip or stop before specific elements. The syntax is currently: #foreach( $foo in $loop.watch($myFoos).skip('badfoo').stop('prematureEndFoo') ) Here's a $foo #end The above would skip any $foo that equals 'badfoo' and stop iteration when the next foo equals 'prematureEndFoo'. Pretty rudimentary, but not too far from your example, particularly if combined with the SortTool. The tricky part of making a $josql tool to do #foreach( $mud in $josql.filter($mudsOnSpecial).where('$customer.hasPurchased($mud)').orderBy('price') ) would be working out the $customer.hasPurchased($mud) part. Though i think that is feasible if you give the tool access to the current context and VelocityEngine so you can evaluate the condition. Then the orderBy can be implemented to sort on $mud properties, just like SortTool can. Anyway, if you're curious about taking the tool route, i'm much more capable of helping out than with modifying the #foreach directive, which would be totally new to me. :) Others here would be of more help with that, if they're not too busy at the moment. On Jan 15, 2008 4:45 AM, Gary Bentley <[EMAIL PROTECTED]> wrote: > Hi, > > I'm the developer of JoSQL (http://josql.sf.net) and I've had a request > from a user to extend the foreach directive to include JoSQL processing > facilities. > > Along the lines of: > > #foreach( $mud in $mudsOnSpecial where $customer.hasPurchased($mud) order by > $mud.price) > > <tr> > <td> > $flogger.getPromo( $mud ) > </td> > </tr> > > In essence to allow the collection to be filtered and ordered prior to being > processed by the foreach directive. > Alternatively it may be that I would add it via a custom directive. > > To make this work though I have a couple of questions: > > 1. How do I make a new directive available to velocity? I am currently, for > testing purposes, using the "ant docs" command > to process a file that has the extended syntax in it. I have modified the > "directive.properties" file to add my new > class, it is loaded and I've modified the value returned by the "getName" > method of my class however when I then use the > directive in my test file it isn't being used. > > 2. How do I get the arguments for the directive from the Node? When I modify > the Foreach class directly I find that only 4 > child nodes are added to the Node, basically no more children are available > after the "where" keyword. (To make argument > processing work I've modified the Parser.jjt file to allow for more args for > the foreach directive). How can I find > out what the arguments are and how can I get the parser to make them > available? They are available if I iterate through > the tokens associated with the Node. > > Many thanks, > > Gary > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]