在Go的text/template模板中,如何在模板本身中将值与值数组进行比较?

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

Go text/template templates: how to check value against an array of values within the template itself?

问题

假设你有一个JSON值,如{ "Fruit": "apple" },作为应用模板之前的输入。我想要检查"Fruit"的值是否在[]string{"pear", "banana", "grape"}这个集合中,并根据值是否在集合中执行某些操作或不执行操作。

所以,模板的输入是:

{ "fruit": "apple" }

模板(假设containsVal是我们传递给模板的自定义函数,它接受一个字符串和一个字符串切片):

{{ if containsVal .Fruit []string{"banana", "grape", "etc"} }}do stuff{{ end }}

似乎模板不允许在其中使用字符串切片字面量,因此模板无法编译通过。

显然,你可以定义一个结构体并将其传递给.Execute()方法。或者,你可以在containsVal函数中硬编码你的值。

但是,对于我的目的,我希望这些值是动态的并且模板中,而不是在Go代码中硬编码。因此,其他人应该能够通过更新模板文本来使用不同的值进行检查(如"fig"、"cherry"等)。

我已经在https://pkg.go.dev/text/template和Google上搜索过,但没有找到任何实现这一目的的方法。我只能在模板中进行简单的相等性比较,比如字符串 == 字符串。

谢谢。

英文:

Let's say you have a JSON value like { "Fruit": "apple" } as your input before applying the template. I want to check that the value of "Fruit" is in a set of []string{"pear", "banana", "grape"} and do something or not do something depending on whether the value is in the set.

So, input to template:

{ "fruit": "apple" }

Template (assume containsVal is a custom function we pass in to the template that accepts a string and a slice of strings):

{{ if containsVal .Fruit []string{"banana", "grape", "etc"} }}do stuff{{ end }}

It seems the templates don't allow string slice literals in them -- template fails to compile.

Apparently you can define a struct and pass that into .Execute(). Or I could hard-code my values in the containsVal function.

But for my purpose, I want the values to be dynamic and in the template, not hard-coded in Go code. So someone else should be able to come along and have a different set of values to check against ("fig", "cherry", etc.) by updating the template text.

I've poked around https://pkg.go.dev/text/template and google and am not seeing any way to do this. I have only been able to do simple equality against simpler variables, like string == string in the templates.

Thanks.

答案1

得分: 4

你应该使用"if"语句与"or"和"eq"运算符,如下所示:

tmplText := `
{{ if eq .Fruit "banana" "grape" "etc" }}
    {{ .Fruit }} 存在于列表中
{{ else }}
    {{ .Fruit }} 不存在于列表中
{{ end }}
`

tmpl, err := template.New("").Parse(tmplText)
if err != nil {
    panic(err)
}

err = tmpl.Execute(os.Stdout, map[string]interface{}{
    "Fruit": "apple",
})
if err != nil {
    panic(err)
}

这段代码使用了模板引擎,根据传入的"Fruit"值判断是否存在于列表中,并输出相应的结果。

英文:

You should use "if" statement with "or" and "eq" operators like as:

	tmplText := `
	{{ if eq .Fruit "banana" "grape" "etc" }}
		{{ .Fruit }} exists in the list
	{{ else }}
		{{ .Fruit }} doesn't exist in the list
	{{ end }}
`

	tmpl, err := template.New("").Parse(tmplText)
	if err != nil {
		panic(err)
	}

	err = tmpl.Execute(os.Stdout, map[string]interface{}{
		"Fruit": "apple",
	})
	if err != nil {
		panic(err)
	}

huangapple
  • 本文由 发表于 2022年7月22日 08:47:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/73074005.html
匿名

发表评论

匿名网友

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

确定