我刚刚开始使用 Google Closure 进行一些工作,我想将选择字段的创建移至其自己的模板中,并使用以下内容调用该模板:
{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}
以及 templates.utils.select 模板将具有为正确选项设置 selected
属性的逻辑。不幸的是,我得到一个“并非所有代码都采用 Soy V2 语法(找到标记 {{param options: [ {name: ...”)异常。
我想我可以使用一种解决方法,将 options 参数传递到调用模板中,但是我需要确保涵盖进入模板的所有方式,这会变得非常乏味,
目前我认为我必须这样做,
<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>
这也很乏味,但至少数据是。有
更好的方法吗?
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)
自 2011 年 9 月 19 日版本起,Closure Templates 提供了对列表的支持和地图文字。不幸的是,该版本中的列表和地图文字存在一个小错误,即 在plovr中部分修复,但官方修复应该很快就会出现,它将被集成到plovr 一旦可用。
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.
不幸的是,不,闭包模板不支持文字列表或映射值,尽管您可以利用 plovr 来解决您的问题(见下文)。就在 Soy 模板中构建值而言,您受到 表达式 部分。
遗憾的是,听起来映射和列表文字在优先级列表上似乎不够高 根据 Closure Templates 的维护者 Kai Huang 的说法。
不过,如果您使用 plovr,那么您可能需要利用
soy-function- plugins
选项,当org.plovr.soy.function.PlovrModule
时,可以通过自定义list()
函数使用列表使用功能插件。 ListFunction 的 Javadoc 显示了一个如何在闭包模板中使用list()
函数的示例。请注意,如果您想在 plovr 之外使用
list()
函数插件,则必须将ListFunction.java
与 闭包模板文档中所述的函数插件。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 customlist()
function when theorg.plovr.soy.function.PlovrModule
function plugin is used. The Javadoc forListFunction
shows an example of how thelist()
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 useListFunction.java
with function plugins as described in the Closure Templates documentation.