Skip to content

Function similar to nindent but ignoring content's initial new line. #357

@ksa-real

Description

@ksa-real

The ask is to have a function that acts like nindent, but skips the content's leading new line. The same effect is if the function act s as indent but doesn't indent the first line. This would allow templates to generate a leading new line that is useful in a lot of scenarios. The proposed name is snindent or sindent.

Explanation

A common way of using templates:

env:
  {{- include "my.multiline" . | nindent 2 }}
extra:
  field: abc

This assumes that "my.multiline" template produces a string without leading and trailing new lines. Let the template generate a list of "items" (line per item), each guarded by some condition.

{{- define "my.multiline" }}
{{- if condition1 }}
- line1
{{- end }}
{{- if condition2 }}
- line2
{{- end }}
{{- end }}

It is not possible to keep the two if blocks independent and keep no new lines around generated string for all possible values of boolean condition1 and condition2

So, the best for template is to generate a new line character per line: leading or trailing. If using trailing, then {{ include "sometemplate" . }} would generate likely unnecessary extra new line. {{ include "sometemplate" . -}} would in turn affect the next line indentation. So, a leading new line is the only option.

Workarounds

env:
  {{- include "my.multiline" . | trimPrefix "\n" | nindent 2 }}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions