Again, for posterity’s sake…
In the video I’m watching, “A Crash Course on Go Templates”, the presenter has a very good explanation of the difference between the “.” (“dot”) and “$” (“dollar”) symbols as used within Hugo/Go Templates:
A Crash Course on Go Templates: time code = 13min/10sec.: the gist of it is:
BOTH .
and $
(NOTE: just $
, the dollar sign on its own and not $someVar
) refer to the context variable that is passed into a given template. The difference is, the .
can change what it’s pointing/referring to within a template based upon whether it is being used in EITHER a with
or range
“action”. For example, the value that .
is pointing to can change within with
and range
template actions. On the other hand, the $
symbol ALWAYS refers to the ORIGINAL context that was passed into a template and it never changes.
Examples:
file: layouts/_default/baseof.html
has calls a “partial” with the file name, “partials/some-partial.html” like so:
{{- /* file: layouts/_default/baseof.html */ -}}
{{ $myFullName := slice "Abraa" "Cadabra" "Dobrinda" }}
{{ partial "some-partial.html" $myFullName }}
then, within partials/some-partial.html
{{- /* `.` refers to the slice, `$myFullName` that was passed into the partial template. */ -}}
{{ delimit . " " }}<br>{{- /* Outputs "Abraa Cadabra Dobrinda" */ -}}
<!-- `$` ALSO refers to the slice, `$myFullName` that was passed into the partial template. */ -}}
{{ delimit $ " " }}<br>{{- /* Outputs the same as `.` "Abraa Cadabra Dobrinda" */ -}}
{{ range . }}
{{- /*
In here, the `.` is referring to each individual element that the slice $myFullName has in it.
Thus, it will render "Abraa" then "Cadabra" then "Dobrinda" individually. NOTE: here, the `.` "dot"
context IS NO LONGER A SLICE and thus, cannot be used with the "collections.Delimit" function.
*/ -}}
{{ . }}<br>
{{- /*
Conversely, the `$` STILL refers to the original/"root level context" that was passed in, namely:
$myFullName. Thus, it will render "Abraa Cadabra Dobrinda" as a full string for every iteration
of the range's loop.
*/ -}}
{{ delimit $ " " }}<br>
{{ end}}
So the total output of of layouts/some-partial.html
should look something like this:
Abraa Cadabra Dobrinda<br>
Abraa Cadabra Dobrinda<br>
Abraa<br>
Abraa Cadabra Dobrinda<br>
Cadabra<br>
Abraa Cadabra Dobrinda<br>
Dobrinda<br>
Abraa Cadabra Dobrinda<br>
I hope this helps someone else… I think it would be helpful to move this reply into it’s own thread but am unsure of how or whether to do that.
Cheers!