Skip to content

text/template: add "else with" like "else if" #57646

@willfaught

Description

@willfaught

The text/template doc says:

{{if pipeline}} T1 {{else if pipeline}} T0 {{end}}
	To simplify the appearance of if-else chains, the else action
	of an if may include another if directly; the effect is exactly
	the same as writing
		{{if pipeline}} T1 {{else}}{{if pipeline}} T0 {{end}}{{end}}

However, no such "else with" counterpart exists:

{{with pipeline}} T1 {{else}} T0 {{end}}
	If the value of the pipeline is empty, dot is unaffected and T0
	is executed; otherwise, dot is set to the value of the pipeline
	and T1 is executed.

Which would be very useful:

{{ with .foo.bar.baz }}
  {{ . }}
{{ else with .foo.bar.bez }}
  {{ . }}
{{ else with .foo.bar.biz }}
  {{ . }}
{{ end }}

Currently, we must do the nesting that the else/if doc mentions:

{{ with .foo.bar.baz }}
  {{ . }}
{{ else }}
  {{ with .foo.bar.bez }}
    {{ . }}
  {{ else }}
    {{ with .foo.bar.biz }}
      {{ . }}
    {{ end }}
  {{ end }}
{{ end }}

This gets tiresome.

Mixing the two would also be useful:

{{ if .foo.bar.baz }}
  {{ .quux }}
{{ else with .foo.bar.bez }}
  {{ . }}
{{ else if .foo.bar.biz }}
  {{ .boz }}
{{ end }}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixPendingIssues that have a fix which has not yet been reviewed or submitted.FrozenDueToAgeProposalProposal-Acceptedearly-in-cycleA change that should be done early in the 3 month dev cycle.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions