I will try to make this as simple as possible. I have two variables in Golang which are parsed to the template file.
Here is my Golang code which declares the variables:
for _, issue := range issues {
issueIDStr := strconv.Itoa(*issue.ID)
parse[*issue.ID] = issueIDStr
parse[issueIDStr+"-label"] = "blah blah"
}
Then in my HTML file:
{{ range .issues }}
<!-- Here I want to use the current issue's ID as a global variable which is outside the range loop -->
<!-- According to the Golang doc which says the following:
When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.
I can use $.Something to access a variable outside my range loop right...
but how can I use a template variable as a global variable? I tried the following which doesn't work.
{{ $ID := .ID }}
<p>{{ index $.$ID "author" }}</p>
{{ end }}
After running this code, I get the error: bad character U+0024 '$'
along with a panic thrown.
Is what I am attempting completely impossible currently, or is there some trick I am missing?
Thanks :)
You should simply make a map[string]interface{} containing the issues array and then use that as the template execution data.
Then you can loop over the issues and directly access its' members from the template.
Here's a small complete example:
Which outputs:
Additionally if you want/need to add data specific to the template rendering process you should define a "rich" issue struct for this purpose and transform your model before passing it to template execution. This could be done for both statically known extra data (as simple members of RichIssue) and for dynamically loaded data (as key/values of a map).
Here's an extended example showing the above suggestions:
Which produces the following output: