开发者

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜