How to refer to the parent?

In my template, I have something like this

{{ range . }}
    {{ .date }}
    {{ range .images }}
        <img src="{{ .thumb }}" title="{{ .date }}" />
    {{ end }}
{{ end }}

As is expected, {{ .date }}, inside the img tag doesn’t work since it’s something that exists higher up in the hierarchy. So how do I refer to it?

You can try to setup the value before going in the image.

{{ range . }}
    {{ $mydate := .date }}
    {{ range .images }}
        <img src="{{ .thumb }}" title="{{ $mydate }}" />
    {{ end }}
{{ end }}

I thought I did this exact same thing beforehand and it wasn’t working until I realized I forgot to add a colon : before the equal = sign in assigning the variable. That was why it wasn’t working, thanks!

Add to that the error in the console was saying executing "page" at <.>: undefined variable: $date so I thought my variable was not being made available in the lower context where I needed to use it.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.