go-templates

How can you call a helm 'helper' template from a subchart with the correct context?

房东的猫 提交于 2019-12-20 12:35:23
问题 Helm charts define helper templates in _helpers.tpl which are used to create normalized names for the services. The standard form of the template for a service (DNS) name is: {{- define "postgresql.fullname" -}} {{- $name := default .Chart.Name .Values.nameOverride -}} {{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} {{- end -}} If using postgresql as a subchart, how are you supposed to use DNS-based service discovery to refer to it? A common pattern seems to be to copy

Ouput json to http.ResponseWriter with template

不问归期 提交于 2019-12-20 05:47:08
问题 i've this template: var ListTemplate = ` { "resources": [ {{ StringsJoin . ", " }} ] } ` rendered with: JoinFunc := template.FuncMap{"StringsJoin": strings.Join} tmpl := template.Must(template.New("").Funcs(JoinFunc).Parse(ListTemplate)) if i send it to a http.ResponseWriter the output text is escaped. var list []string tmpl.Execute(w, list) how can i write a json this way? 回答1: You shouldn't use Go's template engine (neither text/template nor html/template) to generate JSON output, as the

Simple if not working go template

大憨熊 提交于 2019-12-20 03:19:55
问题 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

How to indent content of included template

ぃ、小莉子 提交于 2019-12-20 03:07:08
问题 I am using go templates to create yaml definitions for kubernetes. I am trying to nest templates but run into issues where I can't re-use a definition simply because the indention is wrong when included. I.e., in one case the contents need indentation but do not in another. How can I control the indention of included content? Example below. I am reusing pod.tmpl, in the first case it can be included as is. In the second case I need to indent the entire contents so it becomes member of service

Unescape css input in HTML

僤鯓⒐⒋嵵緔 提交于 2019-12-20 02:52:41
问题 How do I unescape html? I'm passing a css file into html like this <style>{{.file}}</style> I get this <style>ZgotmplZ</style> I've tried wrapping the field with template.HTML(data), didn't work. 回答1: The Go HTML template package properly excapes CSS. Quoting from the documentation of the template package: The escaping is contextual, so actions can appear within JavaScript, CSS, and URI contexts. "ZgotmplZ" is a special value, it is used as a replacement if the value you're trying to include

detect last item inside an array using range inside go-templates

一个人想着一个人 提交于 2019-12-19 18:57:30
问题 This program outputs simply 1,4,2, but I would like to print 1,4,2. As you can see the comma is printed after each items of an array. package main import "os" import "text/template" func main() { params := map[string]interface{}{ "items": [3]int{1, 4, 2}, } tpl := "{{range $i, $el := .items}}{{$el}},{{end}}" lister, _ := template.New("foo").Parse(tpl) lister.Execute(os.Stdout, params) } Is there a way to change {{range $i, $el := .items}}{{$el}},{{end}} and be sure that last item will print "

Golang template and testing for Valid fields

回眸只為那壹抹淺笑 提交于 2019-12-19 09:03:25
问题 In Go's database/sql package, there are a bunch of Null[Type] structs that help map database values (and their possible nulls) into code. I'm trying to figure out how to test whether a struct field is null, or in other words, when its Valid property is false. The recommended way to print a SQL field is to use the .Value property, like this: <div>{{ .MyStruct.MyField.Value }}</div> This works great. But suppose I have something slightly more complicated, where I need to test the value against

Go template: can't evaluate field X in type Y (X not part of Y but stuck in a {{range}} loop)

筅森魡賤 提交于 2019-12-18 03:16:14
问题 Similar question answered here, but I don't think it solves my problem. Let's say you have the following struct: type User struct { Username string Password []byte Email string ... } Moreover, the URL hasa structure like this: example.com/en/users , where "en" is a URL param that will be passed into the template like this: renderer.HTML(w, http.StatusOK, "users/index", map[string]interface{}{ "lang": chi.URLParam(r, "lang"), "users": users}) And in the HTML template I have the following: {{

Golang code to repeat an html code n times

空扰寡人 提交于 2019-12-17 17:04:08
问题 I am working on golang web app. In that I need to iterate an HTML line n number of times. func index(w http.ResponseWriter, r *http.Request) { tmpl := template.Must(template.ParseFiles("templates/index.html")) n := 5 tmpl.Execute(w, n) } <ul> <li><a href="/?page=1">1</a></li> <li><a href="/?page=2">2</a></li> . . . <li><a href="/?page=n">n</a></li> </ul> How can I implement this? 回答1: To repeat something in Go templates, you may use the {{range}} action. But the {{range}} action expects

Capture or assign golang template output to variable

自古美人都是妖i 提交于 2019-12-17 14:56:01
问题 Within a template, how can I achieve this? {{$var := template "my-template"}} I just get "unexpected <template> in operand" . 回答1: There is no "builtin" action for getting the result of a template execution, but you may do it by registering a function which does that. You can register functions with the Template.Funcs() function, you may execute a named template with Template.ExecuteTemplate() and you may use a bytes.Buffer as the target (direct template execution result into a buffer). Here