In Go templates, accessing parent/global pipeline within range

Using the $ variable (recommended)

From the package text/template documentation:

When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.

As @Sandy points out, it is therefore possible to access the Path in the outer scope using $.Path.

const page = `{{range .Files}}<script src="https://stackoverflow.com/questions/17284222/{{html $.Path}}/js/{{html .}}"></script>{{end}}`

Using a custom variable (old answer)

Found one answer just minutes after posting.
By using a variable, a value can be passed into the range scope:

const page = `{{$p := .Path}}{{range .Files}}<script src="https://stackoverflow.com/questions/17284222/{{html $p}}/js/{{html .}}"></script>{{end}}`

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)