{{ template }} vs. {{ partial }}

No,
but like baseof, template is a Go Template “Argument” (see template package - text/template - Go Packages) rather than a Hugo-specific thing (like partial).

From Go Docs:

{{template "name"}}
	The template with the specified name is executed with nil data.

{{template "name" pipeline}}
	The template with the specified name is executed with dot set
	to the value of the pipeline.

{{block "name" pipeline}} T1 {{end}}
	A block is shorthand for defining a template
		{{define "name"}} T1 {{end}}
	and then executing it in place
		{{template "name" pipeline}}
	The typical use is to define a set of root templates that are
	then customized by redefining the block templates within.```

it works like this:

// in a layout
<div>
    {{ template "myTemplate" . }} // note optional context "pipeline" ... the dot
</div>

{{ define "myTemplate }}
    <p>Hi</p>
    <p>The context is: {{ . }}</p>
{{ end }}

Hugo is using those “internal templates” at some point, but there hasn’t been much talk of them in the context of development.


One thing that I have noticed:

You can send content “programmatically” to different partials using print/printf (see this use case by Forestry : Forestry.io CMS | Tina). It doesn’t seem to work with template (at lease not in the ways that I have tried).

so…

// Assuming that you have files: 
// `partials/blocks/a.html`,  
// `partials/blocks/b.html`, and 
// `partials/blocks/c.html`

{{ $blocks := ( slice "a" "b" "c" ) }}

{{ range .blocks }}
    {{ partial ( printf "blocks/%s.html" . ) }}  // works
{{ end }}

{{ range .blocks }}
    {{ template  . }}  // doesn't works
{{ end }}

{{ define "a" }}
  <div class="">A</div>
{{ end }}
{{ define "b" }}
  <div class="">A</div>
{{ end }}
{{ define "c" }}
  <div class="">A</div>
{{ end }}

1 Like