Skip to content

text/template: add support for variable overwrite (= not :=) #10608

Closed
@bep

Description

@bep

Given this template snippet:

{{ $v := "init" }}
{{ if true }}
    {{ $v := "changed" }}
{{ end }}
v: {{ $v }} {{/* => init */}}

The inner $v is not visible on the outside. := does hint that this is a new variable.

Trying

{{ $v = "changed" }}

Gives

unexpected "=" in operand; missing space?

This is probably as designed, but it would be really nice if there was a way to get the changed $v outside of the conditional.

This is the #1 template question we get over at Hugo (https://github.com/spf13/hugo). We have added a hack to workaround it for now ...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions