Trying to process images, should I revamp my whole structure?

So, I don’t know if you still need the explanation:


{{ range $index, $element := .Pages  }} <-- this bit goes through all the child pages in the list
    {{ $element }} <-- one child page, ie .Pages[$index]
    <br>
    {{ $cover := $element.Resources.GetMatch "cover.jpg" }} <-- grabs the resource matching "cover.jpg"
    {{ with $cover }} <-- checks if $cover exists
        {{ .RelPermalink }} <-- only gets run if 'with' statement is true
    {{ end }}
{{ end }}

So you would modify that to do:

{{ $coverUrl := "" }}
{{ with $cover }}
  {{ $coverResized := $cover.Resize "926x q90" }}
  {{ $coverUrl = $coverResized.RelPermalink }}
{{ end }}
... do stuff with {{ $coverUrl }} here ...

A minor point:

You don’t really need to .Scratch here (unless you really want to…)

Your code above could be rewritten as:

{{ $cover := $element.Resources.GetMatch "cover.jpg" }}
{{ $coverUrl := "" }}
{{ if $cover }}
  {{ $coverResized := $cover.Resize "926x q90" }}
  {{ $coverUrl = $coverResized.RelPermalink }}
{{ end }}
<div class="blog-cover lazy" data-src="{{ $coverUrl }}">
  <a href="{{ .Permalink }}"></a>
</div>
{{ $coverImg := .Page.Resources.GetMatch "cover.jpg" }}
{{ $cover := "" }}
{{ if $coverImg }}
  {{ $resizedCover := $coverImg.Resize "2000x q90" }}
  {{ $cover = $resizedCover.RelPermalink }}
{{ end }}
$cover: {{ $cover }}
3 Likes