Issue with partial and context

Hello.

I’m hitting a weird case where I get error calling len: len of untyped nil.

I have a partial in layouts/_default/partials/press/seen.html:

{{ $len := len $.Site.Data.press.seen.seen }}
{{ range $i, $seen := $.Site.Data.press.seen.seen }}
{{ if (eq $i 0) }}
<div class="row justify-content-center">
{{ end }}
    <div class="col-12 col-sm-3 my-4">
        <div class="press-logo text-center">
            <a href="{{ $seen.url }}" target="_blank" rel="noopener">
                <img src="{{ $seen.image }}" alt="{{ $seen.name }}'s Logo" class="img-fluid d-block mx-auto lazy" width="{{ $seen.imageWidth }}" height="{{ $seen.imageHeight }}">
            </a>
        </div> {{"<!-- .press-logo -->" | safeHTML }}
    </div> {{"<!-- .col -->" | safeHTML }}
{{ if (eq (add $i 1) $len) }}
</div> {{"<!-- .row -->" | safeHTML }}
{{ end }}
{{ end }}

I have no problem calling this specific partial from layouts/_default/index.html with {{ partial "home/seen" . }} which in turns calls {{ partial "press/seen" . }}.

The problem arises when I’m trying to call press/seen from another layout too, with this order:

  1. layouts/_default/press.html layout file:

    ...
    {{ range $j, $tabs := $.Site.Data.press.tabs.tab }}
    {{ $.Scratch.Set "tab-name" (.name | urlize | lower) }}
    <div id="{{ $.Scratch.Get "tab-name" }}" role="tabpanel" class="tab-pane fade{{ if (eq $tabs.name "About") }} show active{{ end }} {{ $.Scratch.Get "tab-name" }}-tabpanel">
        {{ partial (printf "press/tabs/%s" ($.Scratch.Get "tab-name")) . }}
    </div>
    {{ end }}
    ...
    
  2. This works OK until I call {{ partial "press/seen.html" . }} in one of the partials from step 1. The files do exist though.

    ←[?25lBuilding sites … ERROR 2018/04/12 18:00:53 Error while rendering "page" in "": template: _default/press.html:27:27: executing "_default/press.html" at <partial (printf "pre...>: error calling partial: template: partials/press/tabs/foo.html:11:19: executing "partials/press/tabs/in-the-media.html" at <partial "press/seen....>: error calling partial: template: partials/press/seen.html:1:11: executing "partials/press/seen.html" at <len .Site.Data.press...>: error calling len: len of untyped nil
    

The data file is there and works fine for the homepage. The exact same partial press/seen.html also works fine there. So, I can rule out the possibility that I have a broken data file.

Trying to debug this, after I removed the call to the partial in foo.html, I added a couple of Context: {{ . }} calls.

In layouts/_default/press.html I get: Context in press layout: map[name:foo]
In layouts/_default/partials/press/foo.html I get: Context in foo partial: map[name:foo]

Not sure if this helps at all. Maybe I’m missing something someone else has hit.

This is with Hugo 0.38.2 on Windows 7 64-bit.

Thanks in advance for any help!

PS. I tried to explain this as good as I could, but it might not be be very clear. In that case please let me know what else I can provide to debug this.

There is something I don’t quiet understand.

From layouts/_default/press.html you seem to be ranging on a data file, which means, the context of this range is whatever is stored in your $.Site.Data.press.tabs.tab.

Passing this as context to your partial… The call to $.Site from inside it should spun an error… Maybe the len error blocks the other one from showing in your terminal…

Do you have a repo?

First of all, thanks for the reply!

Unfortunately, I don’t have a public repo for this :frowning:

I do have an error calling len: len of untyped nil; it’s like it’s empty.

Do you know if there is any way to make this work?

OK, I just figured it out. Not sure if there’s any other “proper” way…

I basically saved and passed the . context in layouts/_default/press.html:

...
{{ $ctx := . }}
{{ range $j, $tabs := $.Site.Data.press.tabs.tab }}
{{ $.Scratch.Set "tab-name" (.name | urlize | lower) }}
<div id="{{ $.Scratch.Get "tab-name" }}" role="tabpanel" class="tab-pane fade{{ if (eq $tabs.name "About") }} show active{{ end }} {{ $.Scratch.Get "tab-name" }}-tabpanel">
    {{ partial (printf "press/tabs/%s" ($.Scratch.Get "tab-name")) $ctx }}
</div>
{{ end }}
...

This makes more sense indeed.