如何在模板中添加条件句。

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

How to add conditional sentences in template

问题

如何在Go语言中编写类似以下的条件语句:

文件:view.html

{{ if(var1 == "" && var2 == "") }}
ALL EMPTY
{{else}}
DISPLAY
{{end}}

在Go语言中,条件语句使用ifelse关键字来实现。在给定的示例中,条件语句检查var1var2是否都为空字符串。如果是,则输出"ALL EMPTY";否则,输出"DISPLAY"。请注意,Go语言中的条件语句使用双括号{{}}来包裹条件表达式和代码块,并使用end关键字来结束条件语句。

英文:

How to write a conditional in Go Lang like this:

File: view.html

{{ if(var1 =="" && var2 =="" }}
ALL EMPTY
{{else}}
DISPLAY 
{{END}}

答案1

得分: 9

模板没有运算符,但是它们有一个名为eq的函数,它接受两个参数并在它们相等时返回true,还有一个名为and的函数,它也接受两个参数并在它们都为true时返回true。因此,你可以将代码中的第一行写成:

{{if (and (eq var1 "") (eq var2 ""))}}
英文:

Templates don't have operators, but they have function eq, which takes two arguments and returns true if they are equal, and function and which also takes two arguments and returns true if they're both true. So you could write the first line in your code as:

{{if (and (eq var1 "") (eq var2 ""))}}

huangapple
  • 本文由 发表于 2017年5月29日 18:15:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/44240003.html
匿名

发表评论

匿名网友

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

确定