Gary Bentley 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>
Hi, Gary, since you wrote me that note about trying to get in with ASF
(thanks but no thanks) I looked back on your participation in this list.
I see you posed the above question.
Here's how you do the above in FreeMarker:
<#--
Special foreach replacement macro that
sorts the sequence by a fieldname (the orderByField parameter)
and executes the associated nested block if
the condition is satisfied, where the condition parameter
is a function or method that operates on the item in
the sequence and returns a boolean
-->
<#macro specialForeach sequence condition orderByField>
<#list sequence?sort_by(orderByField) as item>
<#if condition(item)>
<#nested item>
</#if>
</#list>
</#macro>
Example usage:
<@specialForeach sequence=mud condition=customer.hasPurchased
orderByField="price" ; item>
<tr>
<td>
${flogger.getPromo(item)}
</td>
</tr>
</@specialForEach>
Now, I didn't have time to look into how you do this in Velocity.
There's a fairly long discussion after this. Could you summarize it? How
do you do the above in Velocity? You can answer in private or on the
list, but really, it should be the latter, since other people might want
to know, and after all, that's what these lists are for, right?
Cheers,
Jonathan Revusky
--
lead developer, FreeMarker project, http://freemarker.org/
Velocity or FreeMarker: Looking at 5 Years of Practical Experience
http://freemarker.blogspot.com/2007/12/velocity-of-freemarker-looking-at-5.html
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]