0

I defined this block using go HTML templates:

{{define "button"}}<button> . </button>{{end}}

I would like to use this block as a nested block and I would like to pass arguments to it.

Is it possible to pass elements from iteration to the above block?

{{range $val := .}}
  {{template "button" . /* <!╌ I would like to pass $val here ╌> */}}
{{end}}
</body>```

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
KeyB0rys
  • 392
  • 5
  • 13
  • 1
    `{{template "button" $val}}` or `{{range := . }} {{template "button" .}} {{end}}` – icza Oct 10 '22 at 10:23
  • Thanks! Is it possible to pass not only argument from range loop but also entire context? I mean in this case: {{range $val := .fruits}} {{template "button" . /* <!╌ I would like to pass $val here but also everything what is under dot ╌> */}} {{end}} – KeyB0rys Oct 10 '22 at 10:41
  • 1
    `{{range}}` sets the dot to the iteration values. Use the `$` to refer to outer values, e.g. `$.` to the passed element of the (outer) template. – icza Oct 10 '22 at 10:43
  • Is it possible to pass both: outer value and iteration value? – KeyB0rys Oct 10 '22 at 10:53
  • 1
    [How to pass multiple values from template to template?](https://stackoverflow.com/questions/49473199/how-to-pass-multiple-values-from-template-to-template/49475057#49475057) – icza Oct 10 '22 at 11:02

0 Answers0