英文:
Simple if not working go template
问题
所以我正在对来自结构体的布尔值进行简单的if检查,但似乎不起作用,它只是停止渲染HTML。
以下是结构体的示例:
type Category struct {
ImageURL string
Title string
Description string
isOrientRight bool
}
现在我有一个该Category结构体的切片,我使用range来显示它们。
以下是一个结构体的示例:
juiceCategory := Category{
ImageURL: "lemon.png",
Title: "Juices and Mixes",
Description: `Explore our wide assortment of juices and mixes expected by
today's lemonade stand clientelle. Now featuring a full line of
organic juices that are guaranteed to be obtained from trees that
have never been treated with pesticides or artificial
fertilizers.`,
isOrientRight: true,
}
我尝试了多种方式,比如下面的方式,但它们都没有起作用:
{{range .Categories}}
{{if .isOrientRight}}
Hello
{{end}}
{{if eq .isOrientRight true}}
Hello
{{end}}
<!-- 不打印任何内容 -->
{{ printf .isOrientRight }}
{{end}}
英文:
So I am doing a simple if check on a bool from a struct but it doesn't seem to work, it just stop rendering the HTML.
So the following struct is like this:
type Category struct {
ImageURL string
Title string
Description string
isOrientRight bool
}
Now I have a slice of that Category struct which I get to display with a range.
Bellow is an example of one struct:
juiceCategory := Category{
ImageURL: "lemon.png",
Title: "Juices and Mixes",
Description: `Explore our wide assortment of juices and mixes expected by
today's lemonade stand clientelle. Now featuring a full line of
organic juices that are guaranteed to be obtained from trees that
have never been treated with pesticides or artificial
fertilizers.`,
isOrientRight: true,
}
I've tried multiple ways, like below, but none of them worked:
{{range .Categories}}
{{if .isOrientRight}}
Hello
{{end}}
{{if eq .isOrientRight true}}
Hello
{{end}}
<!-- Print nothing -->
{{ printf .isOrientRight }}
{{end}}
答案1
得分: 8
你必须导出你想要从模板中访问的所有字段:将其首字母改为大写的 I
:
type Category struct {
ImageURL string
Title string
Description string
IsOrientRight bool
}
以及对它的每个引用:
{{range .Categories}}
{{if .IsOrientRight}}
Hello
{{end}}
{{if eq .IsOrientRight true}}
Hello
{{end}}
<!-- 不打印任何内容 -->
{{ printf .IsOrientRight }}
{{end}}
每个未导出的字段只能从声明它的包中访问。你的包声明了 Category
类型,而 text/template
和 html/template
是不同的包,所以如果你希望这些包能够访问它,你需要将其导出。
Template.Execute()
返回一个错误,如果你存储/检查了它的返回值,你会立即发现这个问题,因为你会得到一个类似于以下错误的消息:
template: :2:9: executing "" at .isOrientRight: isOrientRight is an unexported field of struct type main.Category
在 Go Playground 上可以看到你代码的一个可运行示例。
英文:
You have to export all the fields you want to access from templates: change its first letter to capital I
:
type Category struct {
ImageURL string
Title string
Description string
IsOrientRight bool
}
And every reference to it:
{{range .Categories}}
{{if .IsOrientRight}}
Hello
{{end}}
{{if eq .IsOrientRight true}}
Hello
{{end}}
<!-- Print nothing -->
{{ printf .IsOrientRight }}
{{end}}
Every unexported field can only be accessed from the declaring package. Your package declares the Category
type, and text/template
and html/template
are different packages, so you need to export it if you want those packages to have access to it.
Template.Execute()
returns an error, should you have stored / examined its return value, you would have found this out immediately, as you'd get an error similar to this one:
> template: :2:9: executing "" at <.isOrientRight>: isOrientRight is an unexported field of struct type main.Category
See a working example of your code on the Go Playground.
答案2
得分: 2
如果生活给你带来了一些带有小写变量名的模板,可能是从用于其他事情的Pug模板源构建而成的,那么有一种方法可以解决这个问题...
你可以使用map[string]interface{}
来保存要传递给模板的值,所以在上面的例子中:
juiceCategory := map[string]interface{}{
"ImageURL": "lemon.png",
"Title": "果汁和混合饮料",
"Description": `探索我们广泛的果汁和混合饮料,满足今天柠檬水摊的客户需求。现在推出一整套有机果汁,保证从未经过杀虫剂或人工肥料处理的树木中获得。`,
"isOrientRight": true,
}
现在就不需要修改你的模板了...
英文:
If life inflicts templates on you which for some reason have lower-case variable names - perhaps constructed from a Pug template source that is also used for other things - there is a way around this issue...
You can use a map[string]interface{}
to hold the values to be passed into the template, so in the example above:
juiceCategory := map[string]interface{}{
"ImageURL": "lemon.png",
"Title": "Juices and Mixes",
"Description": `Explore our wide assortment of juices and mixes expected by
today's lemonade stand clientelle. Now featuring a full line of
organic juices that are guaranteed to be obtained from trees that
have never been treated with pesticides or artificial
fertilizers.`,
"isOrientRight": true,
}
and now there is no need to change your template...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论