标签: go-templates

如何通过 Helm Chart 将字符串列表(切片)呈现为 yaml 文件中的列表

我有一个字符串数据列表或(切片),例如[string1, string2, string3]。我想在 yaml 文件中以列表方式呈现它

- string1
- string2
- string3
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

我努力了

{{- range $val := $list }}
  - {{ $val }}
{{- end }}
Run Code Online (Sandbox Code Playgroud)

但它将以下内容呈现为多行字符串

- |-
   - string1
   - string2
   - string2
Run Code Online (Sandbox Code Playgroud)

任何想法?先感谢您

go-templates kubernetes-helm

1
推荐指数
1
解决办法
6881
查看次数

如何渲染“模板的模板”,而不转义每个操作

有谁知道如何使用 渲染“模板的模板” ,其中仅渲染text/template特定操作(即:包裹在 中的内容),其余部分将被视为文字?{{...}}

例如,给定以下模板:

I want to render {{.Foo}}.

but I don't want to render anything on this line, like {{.Bar}} or this template: [{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} for {{ .CommonLabels.job }}

Render {{.Foo}} again.
Run Code Online (Sandbox Code Playgroud)

我想呈现以下输出:

I want to render foo.

but I don't want to render anything on this line, like {{.Bar}} or this template: [{{ .Status | toUpper }}{{ …
Run Code Online (Sandbox Code Playgroud)

templates go go-templates

1
推荐指数
1
解决办法
142
查看次数

如何通过模板中的索引获取字段?

我发送一片articles模板.每个article结构都像:

type Article struct {
    ID        uint32        `db:"id" bson:"id,omitempty"` 
    Content   string        `db:"content" bson:"content"`
    Author    string        `db:"author" bson:"author"`
    ... 
}
Run Code Online (Sandbox Code Playgroud)

我可以articles在a中循环切片{{range $n := articles}}并获得每个切片,{{$n.Content}}但我想要的只是在标题中使用第一个(在范围循环之外).我尝试的是:

{{index .articles.Content 0}}
Run Code Online (Sandbox Code Playgroud)

但我得到:

模板文件错误:template:articles_list.tmpl:14:33:在<.articles.Content>处执行"content":无法评估类型interface {}中的字段内容

如果我只是调用

{{index .articles 0}}
Run Code Online (Sandbox Code Playgroud)

它显示了整篇文章[0]对象.

我怎样才能解决这个问题?

go go-templates

0
推荐指数
2
解决办法
776
查看次数

Golang模板中的字符串切片范围

我有一个结构,其中包含一片像下面这样的字符串类型。

 type Data struct {
      DataFields []string
 }
Run Code Online (Sandbox Code Playgroud)

在我的html模板文件中,我希望覆盖字符串切片。但是,各个字段只是没有任何结构名称的字符串。如何遍历包含简单类型(例如字符串,整数等)的切片?

go go-templates

0
推荐指数
2
解决办法
3081
查看次数

Golang 模板顺序

有没有办法使模板顺序无关紧要。

这是我的代码:

var overallTemplates = []string{
    "templates/analytics.html",
    "templates/header.html",
    "templates/footer.html"}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
    render(w,
        append([]string{"templates/home.html"}, overallTemplates...),
        nil)
}

func render(w http.ResponseWriter, files []string, data interface{}) {
    tmpl := template.Must(template.ParseFiles(files...))
    err := tmpl.Execute(w, data)

    if err != nil {
        fmt.Printf("Couldn't load template: %v\n", err)
    }
}
Run Code Online (Sandbox Code Playgroud)

它有效,但如果我将顺序更改overallTemplates为:

var overallTemplates = []string{
    "templates/header.html",
    "templates/footer.html",
    "templates/analytics.html"}
Run Code Online (Sandbox Code Playgroud)

我得到了一个空白页面,因为analytics.html内容是一样的东西{{define "analytics"}}...{{end}}和它是由被称为footer.html{{define "footer"}}{{template "analytics"}} ...{{end}}

go go-templates

0
推荐指数
1
解决办法
388
查看次数

为什么传递的变量不能在html / template中呈现?

我不知道为什么传递的变量没有用html / template呈现

这是在浏览器中呈现的内容:

这是所有传递的变量:[0xc0000a8ec0 0xc0000a8f80 0xc0000a9040 0xc0000a9100]

城市人口州国家首都

这是日志:
$ go运行main.go

2019/11/27 11:00:39 **** => &city has &main.City{Name:"Washington D.C.", State:"", Country:"USA", Capital:false, Population:680000} before appending to cities ****
2019/11/27 11:00:39 **** => &city has &main.City{Name:"Los Angeles", State:"CA", Country:"USA", Capital:false, Population:3900000} before appending to cities ****
2019/11/27 11:00:39 **** => &city has &main.City{Name:"San Francisco", State:"CA", Country:"USA", Capital:false, Population:860000} before appending to cities ****
2019/11/27 11:00:39 **** => &city has &main.City{Name:"Tokyo", State:"", Country:"Japan", Capital:true, Population:9000000} before appending to cities ****
**** …
Run Code Online (Sandbox Code Playgroud)

go slice go-templates go-html-template google-cloud-firestore

0
推荐指数
1
解决办法
39
查看次数

转到struct的模板

我有一个Go模板应该解析为一个结构.如何将bytes.Buffer模板执行功能的结果转换回struct.操场

package main

import (
    "bytes"
    "encoding/gob"
    "fmt"
    "log"
    "text/template"
)

type Data struct {
    Age      int
    Username string
    SubData  SubData
}
type SubData struct {
    Name string
}

func main() {
    s := SubData{Name: "J. Jr"}
    d := Data{Age: 26, Username: "HelloWorld", SubData: s}
    tmpl := "{{ .SubData }}"
    t := template.New("My template")
    t, _ = t.Parse(string(tmpl))
    buffer := new(bytes.Buffer)
    t.Execute(buffer, d)
    fmt.Println(buffer)

    // writing
    enc := gob.NewEncoder(buffer)
    err := enc.Encode(s)
    if err != …
Run Code Online (Sandbox Code Playgroud)

go go-templates

-1
推荐指数
1
解决办法
94
查看次数

无法在没有解析错误的情况下在Google中添加时间

作品:

{{ $temp := timestampToDate $var.date }}
{{ $temp.Format 2006/01/02 }}
Run Code Online (Sandbox Code Playgroud)

不行

{{ $temp := timestampToDate $var.date }}
{{ $temp := $temp.AddDate(0,-1,0) }}    
{{ $temp.Format 2006/01/02 }}
Run Code Online (Sandbox Code Playgroud)

它说它不能用第二行解析文件,但问题是什么?就我所见,我正确使用命令.

go go-templates

-3
推荐指数
1
解决办法
48
查看次数