Why isn't variable passing to partial?

Desired Behavior:

I am trying to set a condition that determines which partial is called based on whether the context is the home page or not.

Current Behavior:

I have an index page where I declare a variable based on whether the context .isHome or not.

Problem:

However, I have not successfully been able to get .IsHome to evaluate to True, nor have I been able to pass $setJumbotronOverlay as a variable to partial Jumbotron.

My code is below…


My index.html:

{{ define "main" }}

<!-- create variable to submit to jumbotron -->
{{ $setJumbotronOverlay := "false" }}
{{ if .IsHome }}
{{ $setJumbotronOverlay := "true" }}
{{ end }}

{{ partial "jumbotron" (dict "imageUrl" .Params.image "title" .Title "setJumbotronOverlay" $setJumbotronOverlay "content" .) }}

{{ partial "blog" . }}

{{ end }}

My jumbotron.html Partial

<div class="pt3 h5 cover bg-bottom-right" style="background-image: url('{{ .imageUrl }}')">
    {{ if .set_jumbotron_overlay . }}
        <div class="fl relative_center" style="background-color:white;width:800px;min-height: 500px">
              {{ partial "for_home" . }}
    <!-- all other pages -->
    {{ else }}
      {{ partial "different_page" }}
    {{ end }} <!-- end if statement determining content based on page url -->
</div>

Is there an obvious mistake that I am making in using if statements, .isHome, or passing variables?

Also, if anyone has a better suggestion for achieving my end goal, I would appreciate some advice.

It should be {{ $setJumbotronOverlay = "true" }} inside the if block, not :=

Edit:

A few more points:

I don’t know if these are typos, but you also need to pass the context through, ie

{{ partial "for_home" . }} 

or whatever context.

Also, if you just want to pass a bool through, you could probably just pass .IsHome directly (unless you need the variable later on anyway), ie:

{{ partial "jumbotron" (dict ... "setJumbotronOverlay" .IsHome ... ) }}

2 Likes

Hey @pointyfar, thank you for the help. Yes, those missing periods were typos. Fixed.

I made the following edit:

{{ $setJumbotronOverlay := "false" }}
{{ if .IsHome }}
  {{ $setJumbotronOverlay = "true" }}
{{ end }}

i.e. removed : , but this resulted in:

ERROR 2018/10/14 12:09:22 Failed to add template “index.html” in path “/opt/build/repo/site/layouts/index.html”: template: /opt/build/repo/site/layouts/index.html:6: unexpected “=” in operand

Could it be my Hugo version?

Likely so. You need version 0.48 or higher

Hi @ozwronski, I’d give one of @pointyfar’s replies the solution mark instead. He did all the troubleshooting and explaining :slight_smile:

1 Like