From: Keats <kea...@di...> - 2003-05-12 21:52:22
|
I have created a directive version of the TemplateTool called #templet. This lets you define a reusable "Templet" within your template that can be evaluated with different values in the context. (See the example script at the end of this message.) It seems to be working pretty well, but I'm still struggling to find an acceptable syntax for setting context values. Currently it works the same way as the $template tool, i.e., you can: 1) pass an array of values to eval(), which will be put in the context as $arg1, $arg2, etc: $templet.eval([$x, $y, $z]) 2) pass an array of values and an array of names to eval(), so the values will get put into the context as $name1, $name2, etc: $templet.eval([$x, $y, $z], ["name1", "name2", "name3"]) 3) pass a Map to eval() whose contents get copied into the templet context. (This requires you to have the Map already): #set $map.name1=$x #set $map.name2=$y #set $map.name3=$z $templet.eval($map) 4) put the values into the templet context directly prior to calling eval(): #set $templet.Context.name1=$x (or equivalently: #set $templet.Args.name1=$x) #set $templet.Context.name2=$y #set $templet.Context.name3=$z $templet.eval() None of the above options are particularly pretty. Perhaps a separate directive for invoking the templet would be better. E.g., #eval $templet #arg $name1=$x #arg $name2=$y #arg $name3=$z I haven't played with subdirectives, but I think I could do this. But I'm not sure this is much of an improvement. A built-in Map declaration syntax would help here. E.g., $templet.eval([name1=$x, name2=$y, name3=$z]) or to follow what I think is the new Velocity syntax: $templet.eval([{"name1" : $x} {"name2" : $y} {"name3" : $z}]) Since I don't see this coming anytime soon, how about a $map function: $map(["name1", $x, "name2", $y, "name3", $z]) and $templet.eval($map(["name1", $x, "name2", $y, "name3", $z])) A #properties directive would be kind of cool: #properties $props { name1: $x name2: $y name3: $z } This could just parse the block and stream it to a Properties.load(). Then you could say: $templet.eval($props) Or how about: #eval $templet using { name1: $x name2: $y name3: $z } Any input would be appreciated. Keats ==================================================== Sample Script: ==================================================== <h3>Example generating a bulleted list using \#templet</h3> #templet $ul { <ul> #foreach $item in $arg1 { <li>$item</li> } </ul> } $ul.eval([["item 1", "another item", "last but not least"]]) <h3>Another list example</h3> $ul.eval([["First", "Second", "Third"]], ["arg1"]) <h3>Yet another</h3> #set $ul.Context.arg1 = ["Larry", "Moe", "Curly"] $ul.eval() |