Is there a parallelism problem with this template logic?

Hi, I want to paginate some custom data(not pages), but I’m worried about whether there will be a parallelism problem with my implementation.

Hugo builds pages in parallel where multiple pages are generated simultaneously. –Build Performance.

Code

// assets/test.html
{{/* Those partials access the page as current context, that is, the DOT MUST be a page variable */}}
{{ partial "header" . }}
{{ partial "breadcrumb" . }}

{{ $pageNum := .Scratch.Get "currentDataPage" }}
{{/* render the custom data of $pageNum */}}

{{ partial "footer" . }}
// layouts/index.html
{{- $tmpl := resources.Get "test.html" }}
{{- $ctx := . }}
{{- range seq 5 }}
    {{- $.Scratch.Set "currentDataPage" . }}
    {{- $testPage := $tmpl | resources.ExecuteAsTemplate (printf "test.%d.html" .) $ctx }}
    <p>
        <a href="{{ $testPage.Permalink }}">{{ . }}</a>
    </p>
{{- end }}

The generated pages are correct, but I’m still worrying about the parallelism issue. Will the {{- range seq 5 }}{{ end }} be built in serial or parallel? If it was built in parallel, does this mean .Scratch.Get "currentDataPage" is uncertain?

Thanks.

EDITED

I need to reuse the partials which require the page as the current context(the dot), that’s why I didn’t pass the custom data to that template.

No, but I try to avoid using a .Scratch whenever possible to make the code easier to understand.

layouts/index.html

{{ $tmpl := resources.Get "test.html" }}
{{ range seq 5 }}
  {{ $ctx := dict "page" $.Page "pageNum" . }}
  {{ $testPage := $tmpl | resources.ExecuteAsTemplate (printf "test.%d.html" .) $ctx }}
  <p>
    <a href="{{ $testPage.Permalink }}">{{ . }}</a>
  </p>
{{ end }}

assets/test.html

{{ partial "header" .page }}
{{ partial "breadcrumb" .page }}

{{/* render the custom data of .pageNum */}}
{{ .pageNum }}

{{ partial "footer" .page }}
2 Likes

Thanks for confirming this and your improved code snippet

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.