Simple if not working go template

大憨熊 提交于 2019-12-20 03:19:55

问题


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:


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:


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...



来源:https://stackoverflow.com/questions/40494828/simple-if-not-working-go-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!