Why IsHome variabe will change value in Scratch get method?

Currently, I were design the post content template and it could use in index.html & single.html that make it more usefully. But then there meet an issue: the variable which name is IsHome , and it’s value will change in home page under loop page list. Show some code for talk:

  • define the scratch var in baseof.html of head:
{{- .Scratch.Set "IsHome" .IsHome }}
  • use this var in some partial template, kind like partial/post/post_meta/post_header.html
{{/* TODO IsHome */}}
  {{ print "Get IsHome value in pages loop: " ($.Scratch.Get "IsHome") }}
  <div class="post-meta-container">
    {{ partial "post/post_meta/created_date.html" . }}
    {{ partial "post/post_meta/update_date.html" . }}
    {{ partial "post/post_meta/categories.html" . }}
    {{- if not (.Scratch.Get "IsHome") }}
    {{ partial "post/post_meta/words.html" . }}
    {{ partial "post/post_meta/readtime.html" . }}      
    {{ partial "post/post_meta/views.html" . }}      
    {{ end }}
  </div>

there need use different display between home page and single page, but the trustly not good, always get the IsHome variable is false.

the repository url is : hugo-theme-next/tree/feature-baseof_layout
and you can use key words TODO IsHome find where them use.

I don’t know what’s happend , in my knowlege that scratch life scope will in all page.
Could you give some tips?
Thanks.

Because in the page loop $paginator.Pages, you directly call a partial template and passing the . dot-context-only. Means only current Page context being iterated passed to the partial template.

in layouts/index.html_:

{{- define "main_class" }}index{{- end }}
{{- define "main" }}
{{/* TODO IsHome */}}
{{- print "Get IsHome value from scratch before paginate: " (.Scratch.Get "IsHome") }}
{{ $paginator := .Paginate (where .Site.RegularPages "Section" "in" .Site.Params.mainSections) }}

{{- range $paginator.Pages }}
++  {{- print "In Here, You Can Still Access Home Page Scratch " ($.Scratch.Get "IsHome) -}}
    {{ partial "post_content.html" . }} {{- /* But not inside post_content.html */ -}}
{{- end }}

{{- partial "partials/pagination.html" . }}  
{{- end }}

1. Custom parameter

use custom parameter, but you need to modify all partials called inside post_content.html scope.

{{- range $paginator.Pages }}
    {{ partial "post_content.html" (dict "Page" . "is_home" ($.Scratch.Get "IsHome)) }} 
{{- end }}

2. Conditional in range (preferred)

call different partial for home page.

{{- range $paginator.Pages }}
    {{ if ($.Scratch.Get "IsHome) }}
       {{ partial "post_content_home.html" . }}
   {{ else }}
       {{ partial "post_content.html" . }}
    {{ end }}
{{- end }}

Thanks you for reply.

At first I had same mind, but there refered so many children partial resources, so give up this idea.

Seems good, but will generate too many duplicate code.

Keep waiting for better ideas.

1 Like

Update:

There found when use top level context in a $ , then could got the Scratch values in range $paginator.Pages loop context, but so sad not working in reference partial children files.

@jmooring @bep look for help, thanks.

Update agin:

That seems I was found the root case because of the Page object will reference the baseof.html file too when index loop pages, the workflow maybe as below:

index <-- baseof.html
index page list loop <-- signle.html <-- baseof.html

So when index loop list pages will reset the IsHome variable in baseof.html content.

But I can’t find the solution. @pamubay maybe your suggest were the only way to solved this problem.

  1. the layouts/index.html only executed for home kind only.
  2. the page $paginator.Pages not executing any of single page template.
  3. in partial template, the $ only reference to the top-most object of current template being executed.

I don’t know whether it is truely, but the fact that I were testing update the variable value in single.html could effected.

As latest testing that were move Scratch function in index.html content before range begined, then I saw the home page list was nil value also dispaly same as the single article display.

index.html

{{- define "main_class" }}index{{- end }}
{{- define "main" }}
{{ $paginator := .Paginate (where .Site.RegularPages "Section" "in" .Site.Params.mainSections) }}
{{ .Scratch.Set "IsHome" .IsHome }}
{{- range $paginator.Pages }}
{{ partial "post_content.html"  . }}
{{- end }}
{{- partial "partials/pagination.html" . }}  
{{- end }}

signle.html

{{- define "main_class" }}page{{- end }}
{{- define "main" }}
{{ partial "post_content.html" . }}
{{- end }}

index.html & signle.html were reused same post_content.html file, maybe it maked issue happened.