Prefer named variables or "with" in templates?

154 views
Skip to first unread message

Paul Baker

unread,
May 26, 2025, 1:13:31 AM (13 days ago) May 26
to golang-nuts
I'm writing a simple Go template. Specifically, it's a Hugo shortcode
that includes a page resource's content into a page, skipping a few
lines.

Both of these approaches work (but piping the output of `after` into
`delimit` doesn't work, because `delimit` takes its arguments in the
wrong order).

Should I prefer one over the other? Which of these (if either) would
be considered idiomatic?

1.
{{- $resource := .Page.Resources.Get $path -}}
{{- $lines := split $resource.Content "\n" | after $skip_lines -}}
{{- delimit $lines "\n" | safeHTML -}}

2.
{{- with .Page.Resources.Get $path -}}
{{- with split .Content "\n" | after $skip_lines -}}
{{- delimit . "\n" | safeHTML -}}
{{- end -}}
{{- end -}}

Nick White

unread,
May 26, 2025, 11:26:42 AM (13 days ago) May 26
to golan...@googlegroups.com
Hi Paul,

On Sun, May 25, 2025 at 03:08:12PM +1000, Paul Baker wrote:
> Should I prefer one over the other? Which of these (if either) would
> be considered idiomatic?
>
> 1.
> {{- $resource := .Page.Resources.Get $path -}}
> {{- $lines := split $resource.Content "\n" | after $skip_lines -}}
> {{- delimit $lines "\n" | safeHTML -}}
>
> 2.
> {{- with .Page.Resources.Get $path -}}
> {{- with split .Content "\n" | after $skip_lines -}}
> {{- delimit . "\n" | safeHTML -}}
> {{- end -}}
> {{- end -}}

I'd consider both to be perfectly reasonable ways of going about
what you're doing. And I'd encourage you to think not "which is the
best way" in general, and instead "which communicates what I am
doing more clearly".

In these short examples it doesn't make much difference, really. And
most of the time I would likely pick example 1, because it is
immediately clear to the reader what is being operated on, and will
remain so even if the `delimit` line is moved further down the
template later.

But if you only ever want to work on these lines in this constrained
bit of the template, and then forget all about them, then 2 has the
advantage of making it clear that you won't be using these variables
for anything else later.

Nick

Paul Baker

unread,
May 27, 2025, 5:41:56 AM (12 days ago) May 27
to golang-nuts
> I'd consider both to be perfectly reasonable ways of going about
> what you're doing. [...] And most of the time I would likely pick example 1

Thanks Nick. Example 1 is my preference as well - I'm a fan of the Zen
of Python's "Explicit is better than implicit" and "Flat is better
than nested".

However most other templates I've seen tend to resemble example 2.
It's good to know that's mostly just preference, not because example 1
is somehow flawed.

Kevin Chowski

unread,
May 27, 2025, 9:30:32 PM (11 days ago) May 27
to golang-nuts

Note that these samples are not semantically equivalent, so that may factor into the choice between the two for more complex examples. While using 'with', if the pipeline value comes out "empty" then the entire 'with' block is skipped. That may have implications in both the short and long term (when considering maintaining this code), in regards to both performance and actual output generated 
Reply all
Reply to author
Forward
0 new messages