In a recent checkin to plovr, I created a new Soy function, list():
http://code.google.com/p/plovr/source/detail?r=42f3b12baec9ae48538233993bcb1121e1f73d12
list() is a vararg function that takes a variable number of arguments
and turns them into a Soy list. As shown in the changeset, I had to
redefine the com.google.template.soy.shared.restricted.SoyFunction
interface to support vararg functions.
The list() function can be used as follows when calling one template
from another:
/***/
{template .base}
{call .listTemplate}
{param items: list('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri',
'Sat') /}
{/call}
{/template}
/** @param items */
{template .listTemplate}
<ul>
{foreach $item in $items}
<li>{$item}
{/foreach}
</ul>
{/template}
Unfortunately, it cannot be used on its own in a {foreach} command:
// THIS DOES NOT WORK
{foreach $item in list('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri',
'Sat')}
<li>{$item}
{/foreach}
I haven't created a map() function yet, but I probably will, though
really, I would prefer to avoid all of this by just having Soy support
all JSON as valid expressions. Technically, this would also mean that
Soy would support double-quoted as well as single-quoted strings.
Can we expect anything like this in future versions of Closure
Templates? Or will I have to continue to fork Closure Templates in
plovr? Specifically, I would like to see:
* Support for array and map literals.
* Support for double-quoted strings.
* Expressions to be valid values for the second operand in a {foreach}
command.
Thoughts?
Michael