在GAE Go模板包中的条件语句

huangapple go评论84阅读模式
英文:

Conditional statement in GAE Go template package

问题

如何在GAE GO的HTML模板中执行条件语句?我试图通过以下方式实现在select标签中选择一个选项:

<select name=".Grade">
          <option value=""></option>
          <option value="1" {{ if .Grade="1" }} selected="selected" {{ end }}>Grade One</option>
          <option value="2" {{ if .Grade="2" }} selected="selected" {{ end }}>Grade Two</option>
          <option value="3" {{ if .Grade="3" }} selected="selected" {{ end }}>Grade Three</option>
          <option value="4" {{ if .Grade="4" }} selected="selected" {{ end }}>Grade Four</option>
          <option value="5" {{ if .Grade="5" }} selected="selected" {{ end }}>Grade Five</option>
          <option value="6" {{ if .Grade="6" }} selected="selected" {{ end }}>Grade Six</option>
</select>

在参考文档中有

{{ if .Grade }} selected="selected" {{ end }} 

但这只在.Grade有值时为真。非常感谢任何帮助!谢谢!

英文:

How is it possible to perform a conditional statement in html template in GAE GO? I was trying to accomplish this to make an option selected in a select html tag:

&lt;select name=&quot;.Grade&quot;&gt;
          &lt;option value=&quot;&quot;&gt;&lt;/option&gt;
          &lt;option value=&quot;1&quot; {{ if .Grade=&quot;1&quot; }} selected=&quot;selected&quot; {{ end }}&gt;Grade One&lt;/option&gt;
          &lt;option value=&quot;2&quot; {{ if .Grade=&quot;2&quot; }} selected=&quot;selected&quot; {{ end }}&gt;Grade Two&lt;/option&gt;
          &lt;option value=&quot;3&quot; {{ if .Grade=&quot;3&quot; }} selected=&quot;selected&quot; {{ end }}&gt;Grade Three&lt;/option&gt;
          &lt;option value=&quot;4&quot; {{ if .Grade=&quot;4&quot; }} selected=&quot;selected&quot; {{ end }}&gt;Grade Four&lt;/option&gt;
          &lt;option value=&quot;5&quot; {{ if .Grade=&quot;5&quot; }} selected=&quot;selected&quot; {{ end }}&gt;Grade Five&lt;/option&gt;
          &lt;option value=&quot;6&quot; {{ if .Grade=&quot;6&quot; }} selected=&quot;selected&quot; {{ end }}&gt;Grade Six&lt;/option&gt;
&lt;/select&gt;

There is

{{ if .Grade }} selected=&quot;selected&quot; {{ end }} 

in the reference doc but this only evaluates to true if .Grade has value. Any help would be highly appreciated. Thanks!

答案1

得分: 18

在基本模板包中没有等式语句。这是一个关于它的有趣讨论。你有几种可能性:

  • 定义一个外部函数来判断相等性,就像Russ Cox在golang-nuts线程中建议的那样,并使用if条件来测试它。
  • 使用基本模板包可以理解的内容(见下面的代码)。
  • 从模板中删除一些逻辑:不是使用6个硬编码字段,你可以构造一个带有selected布尔字段的数据类型,并将6个这些对象的数组传递给一个带有range语句的模板。

我通过使用布尔值的切片来重新创建了你的示例:

func main() {
    temp,err := template.ParseFiles("template.html")
    if err != nil {
        panic(err)
    }

    g := make([]bool, 7)
    g[1] = true
    temp.Execute(os.Stdout, &g)
}

模板中的一行代码如下所示:

<!-- language: html-lang -->
<option value="3"{{ if index . 3 }} selected="selected"{{ end }}>Grade Three</option>

我觉得这看起来不太好。但我想说,所有的解决方案都有各自的缺点,这是一个品味的问题(第三种解决方案应该更清晰,但对于这样一个简单的事情可能被认为是过度设计)。

编辑(2013/12/11)

Go 1.2于2013/12/01发布)中,模板引擎已经更新,并包括了新的运算符,包括比较运算符。现在应该可以正常工作了:

{{if eq .Grade 1 }} selected="selected" {{end}}

不过,你仍然可以选择在模板中尽量少地使用逻辑。

英文:

There is no equality statement in the base template package.
Here is an interesting discussion from golang-nuts about it.

You have several possibilities:

  • define an external function for equality, like the one Russ Cox suggests in the golang-nuts thread and test it with a if condition
  • use something the base template package can understand (see my code below)
  • remove some logic from the template: instead of having 6 hardcoded fields, you could construct a datatype with a selected boolean field and give an array of 6 of these objects to a template with a range statement

I recreated your example by using a slice of booleans:

func main() {
    temp,err := template.ParseFiles(&quot;template.html&quot;)
    if err != nil {
        panic(err)
    }

    g := make([]bool, 7)
    g[1] = true
    temp.Execute(os.Stdout, &amp;g)
}

A line in the template looks like this:

<!-- language: html-lang -->

&lt;option value=&quot;3&quot;{{ if index . 3 }} selected=&quot;selected&quot;{{ end }}&gt;Grade Three&lt;/option&gt;

This doesn't look so good to me. But I'd say that all solutions have their drawbacks and that this is a matter of taste (the third solution should be cleaner but might be considered overkill for such a simple thing).

Edit (2013/12/11)

In Go 1.2 (released on 2013/12/01), the template engine has been updated and includes new operators, including comparison. This now should work as expected:

{{if eq .Grade 1 }} selected=&quot;selected&quot; {{end}}

You can still choose to keep as few logic as possible in your templates, though.

huangapple
  • 本文由 发表于 2013年1月6日 12:40:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/14179359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定