How to use variable in a range statement

Hi, is there anyway to use a variable in a range statement?

For example, I want to do something like

{{ range .Site.Menus.manual }}
    {{ $cur := .Identifier }}
    <h3>{{ .Name }}</h3>
    {{ range .Site.Taxonomies.series.$cur.Pages }}
        ....
    {{ end }}
{{ end }}

So I would like to use something like the $cur variable inside a range statement. Is there anyway to do that?

thx

Jian

1 Like

Something like this?

{{ range (index .Site.Taxonomies.series $cur).Pages }}

Totally untested …

Thanks @bjornerik

I tried this

{{ range .Site.Menus.manual }}
{{ $cur := .Identifier }}
    <h3>{{ $cur }}</h3>
    {{ range (index .Site.Taxonomies.series $cur).Pages }}
        <li>{{ .LinkTitle }}</li>
    {{ end }}
{{ end }}

and got error

ERROR: 2015/03/26 template: theme/partials/side-manual.html:6:29: executing "theme/partials/side-manual.html" at <.Site.Taxonomies.ser...>: Site is not a field of struct type *hugolib.MenuEntry in theme/partials/side-manual.html

Line 6 is {{ range (index .Site.Taxonomies.series $cur).Pages }}

Use $.Site…

Beautiful. That worked. Thanks @bjornerik

Here’s what I got and the sidebar for http://sequencer.io/manual/introduction is now built using this.

<div class="3u">
    <section>
    {{ range .Site.Menus.manual }}
        {{ $cur := .Identifier }}
        <h3>{{ $cur }}</h3>
        <ul class="alt">
        {{ range (index $.Site.Taxonomies.series .Identifier).Pages }}
            <li><a href="{{ .Permalink }}">{{ .LinkTitle }}</a></li>
        {{ end }}
        </ul>
    {{ end }}
    </section>
</div>
1 Like

Looks like you deployed your dev-build.

(and nitpicking: I would use $cur all places or remove it).

I actually removed all $cur in the last push, after the paste here…but thank you!