简单,如果不工作去模板 [英] Simple if not working go template

查看:113
本文介绍了简单,如果不工作去模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在做一个简单的检查,如果从一个结构的布尔,但它似乎并没有工作,它只是停止呈现HTML。



所以下面的struct是这样的:

  type类别结构{
ImageURL字符串
标题字符串
描述字符串
isOrientRight bool
}

现在我分了一部分结构,我可以用范围显示。

Bellow是一个struct的示例:

  juiceCategory:=类别{
ImageURL:lemon.png,
标题:果汁和混合酒,
描述:`探索我们各种各样的果汁和混合饮料,预计今天的
柠檬水摊clientelle。现在推出全系列
有机果汁,保证从
的树木中获得,从未使用杀虫剂或人造
肥料进行处理。,
isorientight:true,
}

我尝试了多种方法,如下所示,但它们都无效:

  {{range .Categories}} 
{{if .isOrientRight}}
您好
{{end}}
{{if eq .isOrientRight true}}
您好
{{end}}

<! - 无需打印 - > ;
{{printf .isOrientRight}}

{{end}}


解决方案

您必须从模板中导出您想要访问的所有字段:将其第一个字母更改为大写 I



  type类别结构{
ImageURL字符串
标题字符串
描述字符串
IsOrientRight bool
}

每一个参考资料:

  {{range .Categories}} 
{{if .IsOrientRight}}
Hello
{{end}}
{{if eq .IsOrientRight true}}
Hello
{{end}}

<! - 无需打印 - >
{{printf .IsOrientRight}}

{{end}}

每个未导出的字段只能从声明包中访问。您的包声明了类别类型,并且 text / template html / template 是不同的包,因此如果您希望这些包有权访问它,则需要导出它。



Template.Execute() 会返回一个错误,如果你已经存储/检查了它的返回值,你会立即发现这个错误,因为你会得到一个类似于这个错误的错误:


template::2:9:在< .isOrientRight>执行:isOrientRight是结构类型main.Category的未导出字段



请参阅 Go Playground

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

解决方案

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.

这篇关于简单,如果不工作去模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆