google-closureplovrgoogle-closure-templates

Is it possible to create objects within Closure templates?


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?


Solution

  • 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.