Templates.ExecuteTemplate(w, "index.html", map[string]interface{} {
"Games": games})
}
Run Code Online (Sandbox Code Playgroud)
游戏在哪里[]map[string]interface{}(sql查询的映射结果)
在模板中:
{{ range $gval := .Games }}
{{ how to make something like: $gval.name.(string) }}
{{end}}
Run Code Online (Sandbox Code Playgroud)
如何将映射的interface{}值转换为模板中的字符串(或int)?在“去”中我可以做
games[0]["name"].(string)
当我这样做时, $gval.name它会写入十六进制字符串
我目前将所有 html 文件都放在一个平面目录中templates/,并使用以下命令加载所有内容
tmpl := template.Must(template.ParseGlob("templates/*.html"))
Run Code Online (Sandbox Code Playgroud)
但我现在想引入一些结构并将模板放入文件夹、、components等中base。但是当我这样做时,我的网站停止工作。我想可能是上面的情况,或者也可能是我需要引用模板中的路径?
例子
{{ template "navbar" }}
Run Code Online (Sandbox Code Playgroud)
会成为
{{ template "components/navbar" }}
Run Code Online (Sandbox Code Playgroud)
有点迷茫...
目前我还使用原生 go 库而不是框架。
我有以下输入:
alerts:
api_http_500:
default_config:
enabled: true
override:
user_1:
enabled: false
Run Code Online (Sandbox Code Playgroud)
我想检查用户是否存在启用的字段。如果不是的话我就使用默认的。我正在使用索引,default如下所示:
{{ (index $alerts "alerts" "api_http_500" "override" "user_2" "enabled") | default (index $alerts "alerts" "api_http_500" "default_config" "enabled") }}
但我有这个错误<index $alerts "alerts" "user_2" "enabled">: error calling index: index of nil pointer。
我不知道如何在没有 if/else 结构的情况下使用默认值。
我想修剪 Go 模板中的空白。我该怎么做呢?
例子:
{{ $title = " My Title of the product " }}
// Print the trim string here
<h1>{{ $title }}</h1>
Run Code Online (Sandbox Code Playgroud) 这个问题与Golang 模板范围换行符删除有关,但那里的答案没有解决。
给定
type Data struct {
Item string
IsFruit bool
}
var data = []Data{
{"banana", true},
{"apple", true},
{"onion", false},
{"peach", true}}
var Template = `
FRUITS ONLY
-----------------
{{range .}}{{if .IsFruit}}{{.Item}}{{else}}{{end}}
{{end -}}
-----------------
`
Run Code Online (Sandbox Code Playgroud)
Template执行with的输出在和data之间包含不需要的换行符。applepeach
FRUITS ONLY
-----------------
banana
apple
peach
-----------------
Run Code Online (Sandbox Code Playgroud)
我尝试了{{-和的许多变体,但-}}没有设法抑制换行符。消除{{else}}也行不通。
请注意,对于我的实际应用程序来说,预过滤数据不是一个选项。
完整代码示例位于 https://goplay.space/#N_GyaWwjH0Y
我希望创建一个 helm 图表来部署具有多个不同 pod 的应用程序(因此有多个部署清单)。这是图表的结构:
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Chart.yaml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 templates\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 NOTES.txt\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 _helpers.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 comp-1\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 comp-1-deployment.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 comp-1-ingress.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 comp-1-service.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 comp-2\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 comp-2-deployment.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 comp-2-service.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 comp-3\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 comp-3-deployment.yaml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 comp-3-service.yaml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 values.yaml\nRun Code Online (Sandbox Code Playgroud)\n我意识到所有组件的模板看起来都一样,所以我想重构以将模板减少为:
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Chart.yaml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 templates\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 NOTES.txt\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 _helpers.yaml\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 comp\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 comp-deployment.yaml\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 comp-ingress.yaml\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 comp-service.yaml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 values.yaml\nRun Code Online (Sandbox Code Playgroud)\n并使用其中的值多次values.yaml渲染以生成它的多个副本。comp我怎么做?我研究过部分,似乎它没有达到我想要的效果。
我的秘密文件如下所示:
apiVersion: v1
kind: Secret
metadata:
name: secret
type: Opaque
stringData:
"user.name": "user"
"user.password": "password"
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用下一个代码获取一个值:
{{- $secret := lookup "v1" "Secret" .Release.Namespace "secret" -}}
{{- if $secret -}}
{{- print $secret.data.user.password}}
Run Code Online (Sandbox Code Playgroud)
问题是“user.password”键包含一个点,我还没有找到如何修复它。谢谢你的帮助!
关于使用字典将多个变量传递到 Helm 中的模板,有多个 stackoverflow 答案。
但是,我想将单个变量传递给模板。例如,我想定义如下模板,接收输入(确切地说是一个字符串)。
{{- define "blahblah" $var }}
{{- if .Values.nameOverride }}
name: {{ .Values.nameOverride }}-$var
{{- else }}
name: $var
{{- end }}
{{- end }}
Run Code Online (Sandbox Code Playgroud)
因此,像下面这样写,我希望结果是name: myname-whateveror name:whatever(假设.Values.nameOverride被定义为“myname”)
{{- include "blahblah" "whatever" }}
Run Code Online (Sandbox Code Playgroud)
如何让 helm 知道哪个是模板的输入变量?谢谢你!
eq 函数允许比较两个字符串是否相等
{{if eq .Name "MyName"}}
Run Code Online (Sandbox Code Playgroud)
有没有办法测试一个字符串是否以(或包含)另一个字符串结尾?
我正在使用Golang模板来管理Helm的部署。
我有这样的values.yaml文件:
env: dev
config:
dev:
myname: Hi
live:
myname: Bye
Run Code Online (Sandbox Code Playgroud)
现在,我想根据环境(开发,实时)获取值。喜欢:
{{ .Values.config. {{.Values.env}} }}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这种方式行不通,因为它说:
bad character U+007B '{'
Run Code Online (Sandbox Code Playgroud)
有什么方法可以使用其他值来获取值?