英文:
Show default content in a template if an object is nil otherwise show based on the set property
问题
在我的模板中,我想要包含一些默认的meta标签(90%的情况下)。然而,当设置了特定的属性时,我想要显示不同的文本。
我知道我可以设置一个匿名的struct,并使用"default"或"some-x"来设置属性。然而,这意味着我需要在90%的处理程序中添加一个匿名的struct,目前只是传递nil。
有没有办法像下面这样做:
{{if eq . nil}} 
   // 默认的meta标签
{{else if eq .MetaValue "some-x"}} 
   // 其他
{{end}}
如果我尝试像上面的代码一样,它可以编译,但不会达到我想要的效果。感谢任何关于如何正确处理它而不添加大量样板代码的建议。
谢谢!
英文:
In my template, I would like to include some default meta tags (90% of the time). However, when a specific property is set, I would like to show a different set of text.
I know I can set an anonymous struct and set a property with either "default" or "some-x". However, this means, I need to add an anonymous struct to 90% of my handlers that just currently pass nil.
Is there way to do something like
{{if eq . nil}} 
   // default meta tag
{{else if eq .MetaValue "some-x"}} 
   //other
{{end}}
If I try something like my above code, it compiles but doesn't do what I want. Appreciate any suggestions on how to handle it properly without adding a lot of boiler plate.
Thanks!
答案1
得分: 24
{{如果不 .}}
当 . 为 nil 或者为空,包括 false、0,以及任何长度为零的数组、切片、映射或字符串时的输出
{{否则如果 .MetaValue 等于 "some-x"}}
// some-x 情况
{{否则}}
// 其他情况
{{结束}}
英文:
{{if not .}}
   output when . is nil or otherwise empty including
     false, 0, and any array, slice, map, or string of length zero
{{else if eq .MetaValue "some-x"}}
       // some-x case
{{else}} 
       // other case
{{end}}
答案2
得分: 10
如果你想确保只检查nil而不是0、false、空字符串或任何其他假值类型,你可以使用kindIs函数来实现这一点。
{{ if kindIs "invalid" . }} 
   // 只有当变量确实为nil时才执行。假值将会被忽略。
{{ else if eq .MetaValue "some-x" }} 
   // 其他情况
{{ else }}
   // 最后一种情况,如果有的话
{{ end }}
英文:
If you want to ensure you're only checking against nil and not 0, false, the empty string, or any other falsey type, you can use the kindIs function to accomplish this.
{{ if kindIs "invalid" . }} 
   // only if variable is literally nil. falsey values will fallthrough.
{{ else if eq .MetaValue "some-x" }} 
   // other
{{ else }}
   // final case, if any
{{ end }}
答案3
得分: 5
我最近遇到了一个问题,无法在Helm Chart(使用Go模板,包括sprig)中区分nil和0值,并且没有找到任何解决方案的帖子,所以我想在这里添加我的解决方案。
我想到了一种有点丑陋的解决方案,就是引用该值,然后检查是否存在与"<nil>"(带引号,所以实际上你将检查(quote .Values.thing | eq ""<nil>""))相匹配的字符串。这样可以区分对空值和定义的0值进行测试。在我的情况下,我正在尝试构建一个配置文件,其中一些默认选项是非0的,因此当明确设置为0时,我想知道是否设置了0,而不仅仅是省略了。
希望这对其他人有所帮助。
希望能有更好的方法来解决这个问题,但到目前为止,我还没有找到任何不需要创建和添加自己的模板函数的方法。
英文:
I've been recently facing an issue with identifying nil vs 0 values in a Helm Chart (which uses Go templates, including sprig) and haven't found any solutions posted, so I thought I'd add mine here.
I came up with a kind of ugly solution which is to quote the value and then check for a string that matches "<nil>" (with quotes, so you'd actually be checking (quote .Values.thing | eq ""<nil>"")). This allows differentiating tests against empty values vs defined 0 values. In my case, I was trying to build a config file where some default options were non-0, so when 0 was explicitly set, I wanted to know that 0 was set instead of just omitted.
Hopefully this can be a help to someone else.
It would be nice to have a better way to do this, but so far I haven't found anything that doesn't require creating and adding my own template functions.
1: https://github.com/Masterminds/sprig "sprig"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论