是否可以在 Closure 模板中创建对象?

发布于 2024-11-18 10:15:10 字数 1191 浏览 2 评论 0 原文

我刚刚开始使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟燃烟灭 2024-11-25 10:15:10

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.

轮廓§ 2024-11-25 10:15:10

不幸的是,不,闭包模板不支持文字列表或映射值,尽管您可以利用 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文