无法在模板中使用ENUM。

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

Can not use ENUM in template

问题

在我的代码中,我定义了一个枚举类型,如下所示:

type flag int

const (
	Admin flag = iota + 1 // iota = 0
	Editer
	Superuser
	Viewer
)

这些信息被传递到模板中,我进行了如下比较:

{{range $i, $v := .Flags}}
	{{if or (eq $v 1) (eq $v 3)}}
		<input type="text" name="subject" placeholder="{{$v}}" required>
	{{else}}
		<input type="text" name="subject" placeholder="{{$v}}" disabled>
	{{end}}
{{end}}

如上所示,比较是通过等于操作符 eq $v 1 进行的,我想做的是类似于 eq $v Admin 这样,即使用枚举名称而不是其值。

我可以这样做吗?

英文:

in my code i defined an enum as below:

type flag int

const (
	Admin flag = iota + 1 // iota = 0
	Editer
	Superuser
	Viewer
)

The info is passed to the template, and I do a comparison like:

        {{range $i, $v := .Flags}}
                {{if or (eq $v 1) (eq $v 3)}} 
                    &lt;input type=&quot;text&quot; name=&quot;subject&quot; placeholder= {{$v}} required&gt;
                {{else}}
                        &lt;input type=&quot;text&quot; name=&quot;subject&quot; placeholder= {{$v}} disabled&gt;
                {{end}}

        {{end}}

As seen here, the comparison is done with the equivalent int eq $v 1, what I like to do is something like eq $v Admin so I use the enum name instead of its value.

Can I do this?

答案1

得分: 3

你可以使用Funcs方法为每个枚举注册模板函数,将它们命名为与枚举常量相同的名称,然后在模板中通过简单地引用它们来调用函数。

例如,要在模板中执行eq $v Admin,可以按照以下方式操作:

type flag int

const (
	Admin flag = iota + 1 // iota = 0
	// ...
)

var funcMap = template.FuncMap{
    "Admin":  func() flag { return Admin },
    // ...
}

var file = `{{ $v := . }}
{{- if eq $v Admin }}is admin{{ else }}is not admin{{ end }}
`

func main() {
	t := template.Must(template.New("t").Funcs(funcMap).Parse(file))

	for _, v := range []interface{}{Admin, 1234} {
		if err := t.Execute(os.Stdout, v); err != nil {
			panic(err)
		}
		fmt.Println("----------------")
	}
}

https://play.golang.org/p/70O7ebuYuNX

is admin
----------------
is not admin
----------------

你还可以在flag类型上声明一个方法,并将method value用作模板函数,使其更加简洁:

type flag int

func (f flag) get() flag { return f }

const (
	Admin flag = iota + 1 // iota = 0
	Editor
)

var funcMap = template.FuncMap{
	"Admin":  Admin.get,
	"Editor": Editor.get,
	// ...
}

var file = `{{ $v := . }}
{{- if eq $v Admin }}is admin{{ else }}is not admin{{ end }}
{{ if eq $v Editor }}is editor{{ else }}is not editor{{ end }}
`

func main() {
	t := template.Must(template.New("t").Funcs(funcMap).Parse(file))

	for _, v := range []interface{}{Admin, Editor, 1234} {
		if err := t.Execute(os.Stdout, v); err != nil {
			panic(err)
		}
		fmt.Println("----------------")
	}
}

https://play.golang.org/p/4JLsqxoHs8H

is admin
is not editor
----------------
is not admin
is editor
----------------
is not admin
is not editor
----------------
英文:

You can register template functions with the Funcs method for each of your enums, given them the same name as the enum's constant has, and then invoke the function within the template by simply referencing them.

i.e. to be able to do eq $v Admin inside a template you can do the following:

type flag int

const (
	Admin flag = iota + 1 // iota = 0
	// ...
)

var funcMap = template.FuncMap{
    &quot;Admin&quot;:  func() flag { return Admin },
    // ...
}

var file = `{{ $v := . }}
{{- if eq $v Admin }}is admin{{ else }}is not admin{{ end }}
`

func main() {
	t := template.Must(template.New(&quot;t&quot;).Funcs(funcMap).Parse(file))

	for _, v := range []interface{}{Admin, 1234} {
		if err := t.Execute(os.Stdout, v); err != nil {
			panic(err)
		}
		fmt.Println(&quot;----------------&quot;)
	}
}

https://play.golang.org/p/70O7ebuYuNX

is admin
----------------
is not admin
----------------

You can also declare a method on the flag type and use the method value as the template function to make it more neat:

type flag int

func (f flag) get() flag { return f }

const (
	Admin flag = iota + 1 // iota = 0
	Editor
)

var funcMap = template.FuncMap{
	&quot;Admin&quot;:  Admin.get,
	&quot;Editor&quot;: Editor.get,
	// ...
}

var file = `{{ $v := . }}
{{- if eq $v Admin }}is admin{{ else }}is not admin{{ end }}
{{ if eq $v Editor }}is editor{{ else }}is not editor{{ end }}
`

func main() {
	t := template.Must(template.New(&quot;t&quot;).Funcs(funcMap).Parse(file))

	for _, v := range []interface{}{Admin, Editor, 1234} {
		if err := t.Execute(os.Stdout, v); err != nil {
			panic(err)
		}
		fmt.Println(&quot;----------------&quot;)
	}
}

https://play.golang.org/p/4JLsqxoHs8H

is admin
is not editor
----------------
is not admin
is editor
----------------
is not admin
is not editor
----------------

huangapple
  • 本文由 发表于 2021年11月10日 03:31:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/69904000.html
匿名

发表评论

匿名网友

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

确定