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

Comments

Projects
None yet
10 participants
@bep

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

@vanburg

This comment has been minimized.

Show comment
Hide comment
@vanburg

vanburg Dec 22, 2015

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

vanburg commented Dec 22, 2015

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

@bep

This comment has been minimized.

Show comment
Hide comment

bep commented Dec 22, 2015

@vanburg

This comment has been minimized.

Show comment
Hide comment
@vanburg

vanburg Dec 23, 2015

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

vanburg commented Dec 23, 2015

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

@jupy

This comment has been minimized.

Show comment
Hide comment
@jupy

jupy Dec 31, 2015

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

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

This comment has been minimized.

Show comment
Hide comment
@bep

bep Dec 31, 2015

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

bep commented Dec 31, 2015

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

@withnale

This comment has been minimized.

Show comment
Hide comment
@withnale

withnale Nov 16, 2017

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

withnale commented Nov 16, 2017

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

@JasonTheAdams

This comment has been minimized.

Show comment
Hide comment
@JasonTheAdams

JasonTheAdams Dec 14, 2017

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!

JasonTheAdams commented Dec 14, 2017

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

This comment has been minimized.

Show comment
Hide comment
@sinnlosername

sinnlosername Dec 17, 2017

Contributor

Anything new?^^

Contributor

sinnlosername commented Dec 17, 2017

Anything new?^^

@mvdan mvdan self-assigned this Dec 17, 2017

@mvdan

This comment has been minimized.

Show comment
Hide comment
@mvdan

mvdan Dec 17, 2017

Member

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.

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 label Dec 17, 2017

@gopherbot

This comment has been minimized.

Show comment
Hide comment
@gopherbot

gopherbot Dec 17, 2017

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

gopherbot commented Dec 17, 2017

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

@mvdan

This comment has been minimized.

Show comment
Hide comment
@mvdan

mvdan Feb 13, 2018

Member

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

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 from text/template: add support for nested variable assignment to Proposal: text/template: add support for nested variable assignment Feb 16, 2018

@gopherbot gopherbot added the Proposal label Feb 16, 2018

@mvdan mvdan removed the NeedsFix label Feb 16, 2018

@mvdan mvdan modified the milestones: Go1.11, Proposal Feb 16, 2018

@mvdan

This comment has been minimized.

Show comment
Hide comment
@mvdan

mvdan Feb 16, 2018

Member

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.

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

This comment has been minimized.

Show comment
Hide comment
@rsc

rsc Feb 26, 2018

Contributor

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.

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 from Proposal: text/template: add support for nested variable assignment to 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 from text/template: add support for nested variable assignment to text/template: add support for overwriting variable assignment Feb 26, 2018

@rsc rsc changed the title from text/template: add support for overwriting variable assignment to text/template: add support for variable overwrite (= not :=) Feb 26, 2018

@mvdan

This comment has been minimized.

Show comment
Hide comment
@mvdan

mvdan Feb 26, 2018

Member

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment