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