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

Support updating variables inside of a loop #222

Closed
sudo-bmitch opened this issue Nov 8, 2016 · 2 comments
Closed

Support updating variables inside of a loop #222

sudo-bmitch opened this issue Nov 8, 2016 · 2 comments

Comments

@sudo-bmitch
Copy link

Background: While working through some nginx-proxy issues, I found that it doesn't behave nicely if a container publishing the trigger variables isn't connected to any common networks with the proxy. I could solve this with a template update, but that update needs to get and set a variable inside of a loop that can be read outside (to open the location on the the first match in the loop, and to close the location block only if it was opened by a match inside the loop).

Feature request: Modifying a variable isn't supported in the go template language (you can replace it, but only in your current scope, which doesn't help with a loop), so helper functions are needed. Hugo has implemented something very similar with Scratch to solve this problem: https://gohugo.io/extras/scratch/. Adding similar functions to docker-gen would be useful to support more complex templates. Further background on the go template limitation can be seen in this issue: golang/go#10608

(Note, this is only a wishlist for me. I've worked around my original issue by making a unique set of trigger variables per nginx-proxy in my environment.)

@rhansen
Copy link
Collaborator

rhansen commented Mar 20, 2022

You can declare a variable outside the loop and update it inside the loop:

{{ $x := "" }}
{{ range $i := .Foo }}{{ $x = $i }}{{ end }}
{{ $x }}

@sudo-bmitch
Copy link
Author

Looks like in the years since I opened this, the upstream Go issue was resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants