英文:
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:
<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>
There is
{{ if .Grade }} selected="selected" {{ 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 arange
statement
I recreated your example by using a slice of booleans:
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)
}
A line in the template looks like this:
<!-- language: html-lang -->
<option value="3"{{ if index . 3 }} selected="selected"{{ end }}>Grade Three</option>
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="selected" {{end}}
You can still choose to keep as few logic as possible in your templates, though.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论