Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to indent content of included template

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

{{ if (eq .Case "pod")
  # NO indenting
  {{ template "pod" }}
{{ end }}

{{ if (eq .Case "service")
  service:
    # need to indent! so contents become members of service:
    {{ template "pod" }}
{{ end }}
like image 790
dirtbikejunkie Avatar asked May 06 '17 15:05

dirtbikejunkie


People also ask

What is indent example?

The definition of an indent is a space that is left when a block of text has been spaced inward further than surrounding text. A space left when you "tab" to move text inward in a word processing program is an example of an indent.

What is Nindent?

nindent. The nindent function is the same as the indent function, but prepends a new line to the beginning of the string.

What is go template?

Go's template is designed to be extended by developers, and provides access to data objects and additional functions that are passed into the template engine programmatically. This tutorial only uses functions universally provided in the text/template package, and does not discuss the specifics of data access.

How do I indent list items in HTML?

Here’s the result compared to a list with default indentation: Like an ordered list element, an unordered list element (<ul>) will indent its list items — or bullet points — by default. If you’d like to change the indentation distance, then you can use the margin-left or padding-left property.

How to indent text in a block using CSS?

You can use the CSS text-indent property to indent text in any block container, including divs, headings, asides, articles, blockquotes, and list elements. Say you want to indent all div elements containing text on a page to the right by 50px. Then, using the CSS type selector div, set the text-indent property to 50px.

How to indent in Microsoft Word?

How to Indent in Word. 1 Method 1. Indenting a Sentence Download Article. 1. Open your document in Microsoft Word. You can do this by double-clicking the file on your computer. 2 Method 2. 3 Method 3.

How do I add a hanging indent to a paragraph?

"Add tab stop at" sets the position for the text following the number on the first line of the paragraph. All measurements are from the left margin. To create a hanging indent, the value for "Aligned at" should be the same as "Add tab stop at."


3 Answers

@Giovanni Bassi's answer only works in helm. The include function is defined in helm here.

Combining with indent from sprig from @tmirks answer, you get:

func renderTemplate(templatePath string, vars interface{}, out io.Writer) error {
    t := template.New(filepath.Base(templatePath))
    var funcMap template.FuncMap = map[string]interface{}{}
    // copied from: https://github.com/helm/helm/blob/8648ccf5d35d682dcd5f7a9c2082f0aaf071e817/pkg/engine/engine.go#L147-L154
    funcMap["include"] = func(name string, data interface{}) (string, error) {
        buf := bytes.NewBuffer(nil)
        if err := t.ExecuteTemplate(buf, name, data); err != nil {
            return "", err
        }
        return buf.String(), nil
    }

    t, err := t.Funcs(sprig.TxtFuncMap()).Funcs(funcMap).ParseFiles(templatePath)
    if err != nil {
        return err
    }
    err = t.Execute(out, &vars)
    if err != nil {
        return err
    }
    return nil
}

then

{{ include "pod" | indent 4 }}
like image 87
s12chung Avatar answered Oct 17 '22 07:10

s12chung


You can indent freely, but you need to use include instead of template, as template is an action and can't be passed to other functions:

{{ include "pod" | indent 4 }}

See the Helm guide for more info.

like image 40
Giovanni Bassi Avatar answered Oct 17 '22 06:10

Giovanni Bassi


You should be able to pipe the output of your template to the indent function available in the sprig package:

{{ if (eq .Case "service")
  service:
    # need to indent! so contents become members of service:
{{ template "pod" | indent 4 }}
{{ end }}
like image 20
tmirks Avatar answered Oct 17 '22 07:10

tmirks