Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bep opened this issue Apr 29, 2015 · 14 comments
Closed

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

bep opened this issue Apr 29, 2015 · 14 comments

Comments

@bep
Copy link
Contributor

bep commented Apr 29, 2015

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 ...

@qd3v
Copy link

qd3v commented Dec 22, 2015

@bep can you please point where this hack located? Searched docs several times, but no luck...
Thanks!

@bep
Copy link
Contributor Author

bep commented Dec 22, 2015

@vanburg http://gohugo.io/extras/scratch/

@qd3v
Copy link

qd3v commented Dec 23, 2015

@bep wow, how can I missed it?! Thanks!

@jupy
Copy link

jupy commented Dec 31, 2015

@bep, could you explain, what is your workaround? How I can assign to $v variable different values depending on condition. tx.

@bep
Copy link
Contributor Author

bep commented Dec 31, 2015

@jupy ask here: http://discuss.gohugo.io/ This is the GO issue tracker.

@withnale
Copy link

Any update on this issue? It's such a simple issue that would make working with templates so much easier.

@JasonTheAdams
Copy link

Wow, I'm amazed that it's been almost two years and this still doesn't work? I thought I was going crazy, not realizing it was a limitation — it seems so obvious. Looking forward to this!

@sinnlosername
Copy link
Contributor

Anything new?^^

@mvdan mvdan self-assigned this Dec 17, 2017
@mvdan
Copy link
Member

mvdan commented Dec 17, 2017

Had a quick look. As @bep said, this seems to be by design - templates have no = or assignment operator. Not in the package godoc, nor anywhere in the text/template/parser package.

This issue isn't a proposal, and it was set to Unplanned which means "might be fixed at some point", so I'll assume that there is no reason to not add the feature. It also seems logical to have it, as the template package does speak of "variables" and the := operator can lead one to think that = is available too.

I'll try to have a CL for 1.11 - reviewers welcome once it's up.

@mvdan mvdan modified the milestones: Unplanned, Go1.11 Dec 17, 2017
@mvdan mvdan added the NeedsFix The path to resolution is known, but the work has not been done. label Dec 17, 2017
@gopherbot
Copy link
Contributor

Change https://golang.org/cl/84480 mentions this issue: text/template: add variable assignments

@mvdan
Copy link
Member

mvdan commented Feb 13, 2018

The 1.11 tree is now open and I am still looking for reviewers - any takers from the emoji party above?

@mvdan mvdan changed the title text/template: add support for nested variable assignment Proposal: text/template: add support for nested variable assignment Feb 16, 2018
@mvdan mvdan removed the NeedsFix The path to resolution is known, but the work has not been done. label Feb 16, 2018
@mvdan mvdan modified the milestones: Go1.11, Proposal Feb 16, 2018
@mvdan
Copy link
Member

mvdan commented Feb 16, 2018

In the CL I posted, Rob pointed out how this idea was never formally a proposal, so it was never reviewed nor accepted. In my earlier comment I explained why I assumed that this issue could be worked on, but I think I was a bit too optimistic in my thinking that it had already been reviewed.

For the time being, I've repurposed the issue as a proposal, and hopefully the proposal review team will get to it soon.

@rsc
Copy link
Contributor

rsc commented Feb 26, 2018

I half-remember putting this into a local fork a long time ago. It does seem like something you'd want if you write any sizable amount of templates.

@rsc rsc changed the title Proposal: text/template: add support for nested variable assignment text/template: add support for nested variable assignment Feb 26, 2018
@rsc rsc modified the milestones: Proposal, Go1.11 Feb 26, 2018
@rsc rsc changed the title text/template: add support for nested variable assignment text/template: add support for overwriting variable assignment Feb 26, 2018
@rsc rsc changed the title text/template: add support for overwriting variable assignment text/template: add support for variable overwrite (= not :=) Feb 26, 2018
@mvdan
Copy link
Member

mvdan commented Feb 26, 2018

Great; back to the CL :) I will reply to Rob's concerns there soon. More reviewers are still needed.

dbnicholson added a commit to endlessm/mirrorbits that referenced this issue Feb 6, 2019
This reverts commit 26a3f96. The
feature to overwrite a variable within a conditional using `=` is new in
Go 1.11[1]. The Go version on bionic is 1.10, so this fails at runtime:

  template: mirrorlist.html:27: unexpected "=" in operand

Since we have enabled mirrors, then I don't think we'll run into the
issue this was fixing anyways.

The revert needs a little adjustment since the label was changed from
the ID attribute to the Name attribute since this commit.

1. golang/go#10608

https://phabricator.endlessm.com/T25314
@golang golang locked and limited conversation to collaborators Apr 4, 2019
alexh-sauce referenced this issue in helm/charts Oct 21, 2019
…gMap. (#18115)

Signed-off-by: Cédric de Saint Martin <cdesaintmartin@wiremind.fr>
@rsc rsc unassigned mvdan Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants