标签: go-templates

如何在 Helm Chart 中使用 if 条件

我在values.yaml中有以下值

  pg_hba:
    - hostssl all all 0.0.0.0/0 md5
    - host    all all 0.0.0.0/0 md5
Run Code Online (Sandbox Code Playgroud)

要求是检查该hostssl行是否存在,如果是,则应进入 if 循环并执行某些操作。

我尝试使用{{ if has "hostssl" .Values.pg_hba }} ,但它仅搜索确切的字符串“hostssll”,而不是整行。

请帮助我如何检查 if 条件下的整行。

go-templates kubernetes-helm

11
推荐指数
1
解决办法
5万
查看次数

相同的代码但使用gin + go-template的结果不同

基本信息

  • 转到版本:go1.4.2 darwin/amd64
  • 操作系统:Mac OS X 10.10.5

我正在开发一个基于gogin编写的小型Web项目.这是我的golang代码.运行后,go run test.go我们有一个Web服务器,正在监听8089.

Golang test.go

package main

import "github.com/gin-gonic/gin"
import "net/http"

func main() {
    router := gin.Default()
    router.LoadHTMLGlob("templates/*")
    router.GET("/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.html", gin.H{
            "scheme": "http",
            "domain": "meican.loc",
        })
    })
    router.Run(":8089") // listen and serve on 0.0.0.0:8089
}
Run Code Online (Sandbox Code Playgroud)

后端生成的html代码应该包含前端javascript引擎使用的模板(比方说Angular.js).

所以模板代码在script标签中,就像这样:

template/index.html的一部分

<script type="text/template" charset="utf-8">
  <div data="{{.scheme}}://{{.domain}}/qr"></div>
  <div data="{{.scheme}}://{{.domain}}/qr"></div> <!-- problem here -->
</script>
Run Code Online (Sandbox Code Playgroud)

{{.domain}}第二次使用时,我得到了不同的结果.我刷新了浏览器并检查了源代码.然后我得到了这个:

浏览器源代码结果

<script type="text/template" charset="utf-8">
  <div data="http://meican.loc/qr"></div>
  <div data="http://"meican.loc"/qr"></div> …
Run Code Online (Sandbox Code Playgroud)

html go go-templates go-gin

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

如何从具有正确上下文的子图中调用helm'helper'模板?

Helm图表定义了帮助程序模板,_helpers.tpl其中用于为服务创建规范化名称.服务(DNS)名称模板的标准格式为:

{{- define "postgresql.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
Run Code Online (Sandbox Code Playgroud)

如果使用postgresql作为子图,您应该如何使用基于DNS的服务发现来引用它?一种常见的模式似乎是将子图助手复制到父图表中.

{{- define "keycloak.postgresql.fullname" -}}
{{- $name := default "postgresql" .Values.postgresql.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
Run Code Online (Sandbox Code Playgroud)

这是完全疯狂的原因显而易见!!!

当然有更好的方法来使用子图助手?如果你可以创建一个上下文对象,那么就可以调用它,例如:

value: {{ template "postgresql.fullname" ({Chart: {Name: 'not-used'}, Release: {Name: .Release.Name}, Values: { nameOverride: .Values.postgresql.nameOverride}}) }}
Run Code Online (Sandbox Code Playgroud)

可悲的是,我不知道如何动态地创建这样的上下文.如果辅助函数更改为引用新属性,这仍然会中断,但显而易见.

或者,从子图中提供服务名称的另一种方法是什么?

go-templates kubernetes-helm

10
推荐指数
2
解决办法
3991
查看次数

使用动态名称调用其他模板

我没有看到使用动态名称调用模板(文本或html)的方法.例:

这有效:

{{template "Blah" .}}
Run Code Online (Sandbox Code Playgroud)

这种错误与模板调用中的"意外"$ BlahVar":

{{$BlahVar := "Blah"}}
{{template $BlahVar .}}
Run Code Online (Sandbox Code Playgroud)

我试图解决的整体问题是我需要根据配置文件有条件地呈现模板 - 所以我不知道模板的名称提前.显然我可以在FuncMap中放置一个函数,它只进行单独的模板解析和调用,并返回该结果但是想知道是否有更好的方法.

go go-templates

9
推荐指数
3
解决办法
4484
查看次数

Golang模板范围中的最后一项

鉴于模板

{{range $i, $e := .SomeField}}
        {{if $i}}, {{end}}
        $e.TheString
{{end}}
Run Code Online (Sandbox Code Playgroud)

这可以输出

one, two, three
Run Code Online (Sandbox Code Playgroud)

但是,如果我想输出

one, two, and three
Run Code Online (Sandbox Code Playgroud)

我需要知道哪个是上面范围内的最后一个元素.

我可以设置一个变量来保存数组的长度.SomeField,但是它总是3,而上面的$ i值只会变为2.你不能在我看到的模板中执行算术运算.

是否可以检测模板范围中的最后一个值?干杯.

go go-templates

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

使用模板内的条件

在模板中使用if语句确实令我感到困惑.

我正在尝试class = "active"使用golang模板创建一个导航列表,以执行检测活动选项卡的基本选项卡菜单.这是我的尝试:

{{define "header"}}
<!DOCTYPE html>
<html>
    <head>
        <title>Geoprod</title>
        {{template "stylesheet" .}}
    </head>
    <body>
        <nav class="navbar" role="navigation">
          <div class="navbar-header">
            <a{{if eq .Active "accueil"}} class="active"{{end}} href="/">Geoprod</a>
          </div>
          <div class="navbar-body">
            <ul class="navbar-list">
                <li{{if eq .Active "societe"}} class="active"{{end}}><a href="/societe">Soci&eacutet&eacute</a></li>
                <li{{if eq .Active "dossier"}} class="active"{{end}}><a href="/dossier">Dossier</a></li>
                <li{{if eq .Active "temps"}} class="active"{{end}}><a href="/temps">Temps</a></li>
                <li{{if eq .Active "mails"}} class="active"{{end}}><a href="/mails">Mails</a></li>
            </ul>
          </div>
        </nav>
{{end}}
Run Code Online (Sandbox Code Playgroud)

在main.go中:

var FuncMap = template.FuncMap{
    "eq": func(a, b interface{}) bool {
        return a == b
    },
}
var …
Run Code Online (Sandbox Code Playgroud)

if-statement go go-templates

9
推荐指数
1
解决办法
2万
查看次数

如何使用struct或variable值字段作为模板名称?

我们可以通过定义模板名称{{define "home"}},然后通过其他(父)模板加载它{{template "home"}}.

我如何通过变量值加载模板{{template .TemplateName}}.或者这不可能?

templates go go-templates

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

在 Hugo 部分模板中,如何访问辅助/附加参数?

说我有{{ partial "li.html" $test $root.Data.Term }}

有了这个,我可以访问第一个参数,或者$test,通过简单地.li.html模板中引用,但是如何$root.Data.Term从同一个模板中访问第二个或附加参数 ( )?

go go-templates hugo

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

如何跨多行分割长golang模板函数?

我在golang模板中有一个很长的printf调用.例如

{{ printf "mongodb://%s:%s@%s/%s?authSource=admin&replicaSet=%s&readPreference=nearest&w=majority" .Values.rocketchat.mongo.username .Values.rocketchat.mongo.password .Values.rocketchat.mongo.database .Values.mongodb-replicaset.replicaSetName | b64enc | quote }}
Run Code Online (Sandbox Code Playgroud)

如何在多行中拆分?就像这样:

{{ printf "mongodb://%s:%s@%s/%s?authSource=admin&replicaSet=%s&readPreference=nearest&w=majority"
    .Values.rocketchat.mongo.username
    .Values.rocketchat.mongo.password
    .Values.rocketchat.mongo.database
    .Values.mongodb-replicaset.replicaSetName
    | b64enc | quote }}
Run Code Online (Sandbox Code Playgroud)

go go-templates

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

How to control indent after including file content in Helm configMap?

I'm setting up a ConfigMap for my Helm chart.

As per good practice, I want to include non-yaml resources through separate files rather than inline. Currently I am trying to include both an xml file and a tpl helper in my ConfigMap under "data". Both are read without issue in the below code. But I cannot seem to make the indentation for the keys work properly.

My ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
    name: {{ template "name" . }}
    labels: …
Run Code Online (Sandbox Code Playgroud)

go-templates kubernetes kubernetes-helm

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