3

I've just started doing some work with Google Closure, and I'd like to move the creation of select fields into its own template, and call into that template with something like:

{call templates.utils.select}
  {param name: 'state'/}
  {param value: $selected_state /}
  {{param options: [
    {name: 'Australian Capital Territory', value: 'ACT'},
    {name: 'New South Wales', value: 'NSW'},
    {name: 'Northern Territory', value: 'NT'},
    {name: 'Queensland', value: 'QLD'},
    {name: 'South Australia', value: 'SA'},
    {name: 'Tasmania', value: 'TAS'},
    {name: 'Victoria', value: 'VIC'},
    {name: 'Western Australia', value: 'WA'}
  ]/}}
{/call}

and the templates.utils.select template would have the logic to set the selected property for the correct option. Unfortunately I get a 'Not all code is in Soy V2 syntax (found tag {{param options: [ {name: ...' exception.

I assume I could use a work-around of having the options parameter passed into the calling template, but then I'd need to ensure that all the ways of getting into the template are covered, which would get very tedious.

At the moment I think I'll have to go with

<select name="state">
  <option value="ACT" {if $selected_state=='ACT'}selected="selected"{/if}>Australian Capital Territory'</option>
  <option value="NSW" {if $selected_state='NSW'}selected="selected"{/if}>New South Wales</option>
  ...
</select>

which is also tedious, but at least the data is in a single place.

Is there a better way?

2 Answers 2

4

As of the Sept 19, 2011 release, Closure Templates provides support for list and map literals. Unfortunately, there is a small bug with list and map literals in the release, which is partially fixed in plovr, but an official fix should be coming soon, which will be integrated into plovr once it is available.

2
  • Yes, I was quite happy to see that mentioned on the plovr mailing list - I'm looking forward to getting my hands on the next release (slightly annoying that they waited a few days after you released the September version of plovr before giving us these new shiny goodies), do you have any idea when the next release will be?
    – Cebjyre
    Oct 13, 2011 at 15:31
  • If you are not on Windows, you can get plovr with support for literals at code.google.com/p/plovr/downloads/… Otherwise, you need to wait until code.google.com/p/plovr/issues/detail?id=51 is fixed. Various folks have submitted patches for the bug, so I just need to integrate it in the cleanest way and things should be OK again.
    – bolinfest
    Nov 22, 2011 at 14:51
3

Unfortunately, no, Closure Templates do not support literal list or map values, though you may be able to leverage plovr to solve your problem (see below). In terms of constructing values within a Soy template, you are limited by what can be expressed in the Expressions section of the Closure Templates documentation.

Sadly, it does not sound as though map and list literals are high enough on the priority list according to the maintainer of Closure Templates, Kai Huang.

Though if you are using plovr, then you may want to leverage the soy-function-plugins option, which makes it possible to use lists via a custom list() function when the org.plovr.soy.function.PlovrModule function plugin is used. The Javadoc for ListFunction shows an example of how the list() function can be used in a Closure Template.

Note that if you want to use the list() function plugin outside of plovr, then you must use ListFunction.java with function plugins as described in the Closure Templates documentation.

2
  • Cheers, and yeah, I'm using plovr. I was able to get what I need with two parallel lists, but I couldn't make a single list that contained objects - was I doing something wrong, or is that just not supported at the moment?
    – Cebjyre
    Jul 6, 2011 at 5:47
  • Sorry, at the moment, I believe list() can only take other valid Closure Templates expressions, so if you are trying to supply a map/object as an argument to list(), then that will not work.
    – bolinfest
    Jul 6, 2011 at 19:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.