1

I have this code I use inside a partial in Hugo to pass context to it.

{{- $ctx := . -}}
    {{- $curPage := .page -}}
    {{- $otherVar := .otherVar -}}
    {{- with $curPage -}}
     {{ $section := .CurrentSection }}
    {{ if .IsHome }}
    <span class="post-section"><a href="{{ $section.Permalink }}" target="_blank">{{ $section.Title }}</a></span>
    {{ else }}
    <a href="{{  $section.Permalink }}"> {{  $section.Title }}</a>
    {{ end }}
    {{- end -}}

I then add {{- $curPage := . -}} at the top of the template where I want the partial to appear, then call the partial as {{ partial "partial-name.html" (dict "page" $curPage "otherVar" .) }}. However, the content returns nil on the homepage while it works everywhere else sitewide. Could anyone look at my code and tell me where I went wrong?

2 Answers2

0

Sorry - didn't see your with statement.

So {{- $curPage := .page -}}

is a typo.

.Page

Tested on local - most present version of hugo.

Also note - I don't think homepage has a section so your span will output very little as most of the currentsection or section related will return nothing.

Rogelio
  • 910
  • 5
  • 14
  • It returned an `error execute of template failed: template: partials/-partial-name.html:8:6: executing "partials/-partial-name.html" at <$this>: can't give argument to non-function $this` –  Jan 28 '22 at 19:35
  • See above. Tested. – Rogelio Jan 28 '22 at 19:55
  • `.Page` returns blank, though I think you are correct in that current section issue. But it seems Hugo's support for nested sections is very limited in terms of variables. I inserted a random link and title in the condition inside `.IsHome` in my code and it showed up. I just cannot figure out how to call a nested section link and title on homepage. –  Jan 28 '22 at 20:26
  • I would suggest you then write a new question... with what exactly you are running into and close this... Also note: .page isn't a variable if .Page is returning blank you have other issues. – Rogelio Jan 28 '22 at 21:43
0

Since you call the partial like this:

{{ partial "partial-name.html" (dict "page" $curPage "otherVar" .) }}
                                                     ^^^^^^^^^^^^
                                                        Notice

The dot (.) is contained in .otherVar. To find out if you are on the home page, use something simple like this at the top of partial-name.html:

{{ if .otherVar.IsHome }}
  <pre>Debugging: YES .IsHome</pre>
{{ else }}
  <pre>Debugging: NOT .IsHome</pre>
{{ end }}

After you test with the above GO HTML fragment, you can update your original code above.

TIP: In the Hugo world, it is common to use "context", "ctx", "page", or "Page" rather than "otherVar" as the name of the dictionary key that contains the dot. For a discussion about this, see Naming convention for page context (within a dictionary) when calling partial: seeking opinions in the Hugo discussion group.

ANOTHER TIP: There are some Hugo weirdnesses related to case sensitivity so I would not use "otherVar" anyway. Instead use "othervar", "context", or any name that is all lower case with no whitespace. I do this because I have spent a lot of time messing around with case sensitivity Hugo issues.

n m
  • 1,601
  • 6
  • 8
  • It returns an error `can't give argument to non-function .context` (changed othervar to context) –  Jan 29 '22 at 23:11
  • Did you put the 5 lines of debugging code I suggested **at the top** of `partial-name.html`? If you put it somewhere below the top, the dot (.) may have changed. For more help about this, please show all your code because I'm just guessing about what might be going on. – n m Jan 31 '22 at 16:25
  • I did. It shows everywhere where the partial is called. I think the conflict is the `.CurrentSection` which returns nil on homepage. –  Feb 01 '22 at 02:22