Created a new function: list()

70 views
Skip to first unread message

bolinfest

unread,
Feb 10, 2011, 8:23:52 PM2/10/11
to Closure Templates Discuss
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

Kai Huang

unread,
Feb 11, 2011, 5:40:29 PM2/11/11
to Closure Templates Discuss
On Feb 10, 5:23 pm, bolinfest <bolinf...@gmail.com> wrote:
> 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.

Map and list literals have been on the todo list since the beginning,
but they never manage to get high enough priority.


> * Support for double-quoted strings.

This will definitely never happen. I'm strongly against having
multiple syntaxes for the same thing (unless we're deprecating the
other syntax).


> * Expressions to be valid values for the second operand in a {foreach}
> command.

This probably goes hand-in-hand with the map and list literals. So if
that ever gets in, then this will happen too.

Kai Huang

unread,
Feb 11, 2011, 5:55:23 PM2/11/11
to Closure Templates Discuss
I should mention that even though we're not working on this particular
feature, it doesn't mean we're not working on Closure Templates. In
fact, we've been doing a lot of work on Closure Templates lately, and
the next release will have some cool improvements.
Reply all
Reply to author
Forward
0 new messages