Johno
1
Hello everyone,
Is it possible to pass a variable in a menu array? I have a menu and want to pass diffrent classes to it depending on where it’s included?
Partial:
{{ partial "menus/main" (dict "class" "foo bar bazz" ) }}
menus/main:
{{- range site.Menus.main }}
<a class="{{ .class }}" href="{{ .URL }}">
{{ .Name }}
</a>
{{- end }}
It gives me an error saying .class
is not there, the code works outside of the range
but not inside of it?! What am I missing?
Many thanks in advance.
Context. Within the range, the dot isn’t what you hope it is.
Johno
3
Oh I see, so the dot stops working when it’s within a range
?
No, the dot means something else within the range. The with
and range
statements rebind the context.
To get to context passed to the template from within a block that has a different context, prefix with $.
{{- range site.Menus.main }}
<a class="{{ $.class }}" href="{{ .URL }}">
{{ .Name }}
</a>
{{- end }}
3 Likes
Johno
5
I tried everything but that ! Thank you so much @jmooring I learnt something new today thanks to you. Appreciate it!