I’m using this bit of code to create header images:
{{ with .Resources }}
{{ $cover := .Resources.GetMatch "header" }}
{{ .Scratch.Set "image" ($cover.Resize "500x") }} {{ $image := .Scratch.Get "image" }}
<img src="{{ with $image }}{{ $image.RelPermalink }}{{ else }}{{ .Site.BaseURL }}images/fractal_thumb.png{{ end }}" alt="{{ $image.Name }}" />
{{ else }}
<img src="{{ .Site.BaseURL }}images/fractal_thumb.png" alt="header" />
{{ end }}
and when I run the build command, I get a thousand errors like this:
ERROR 2018/02/05 19:49:15 in .Render: Failed to execute template "theme/_default/summary.html" for page "stories/paginas-tantas/15-08-2017/index.pt.md"
The front matter of those posts is set like so:
---
categories: ["Creativity"]
date: 2017-08-15T11:24:45+01:00
draft: false
resources:
- src: ""
name: "header"
slug:
subtitle:
title: 15 08 2017
stories: ["paginas-tantas"]
---
Should I be making the check for Resources in some other way? Or is it that I am missing something on how Resources work?
regis
February 5, 2018, 8:48pm
2
You don’t appear to be checking if .Resources.GetMatch "header"
really returns anything.
it was one of my earlier tries and returns the same error.
{{ with .Resources.GetMatch "header" }}
{{ $cover := .Resources.GetMatch "header" }}
{{ .Scratch.Set "image" ($cover.Resize "500x") }} {{ $image := .Scratch.Get "image" }}
<img src="{{ with $image }}{{ $image.RelPermalink }}{{ else }}{{ .Site.BaseURL }}images/fractal_thumb.png{{ end }}" alt="{{ $image.Name }}" />
{{ else }}
<img src="{{ .Site.BaseURL }}images/fractal_thumb.png" alt="header" />
{{ end }}
regis
February 5, 2018, 8:50pm
4
What are you trying to achieve with this ?
This does not match any resource, so none of them will get the name “header”
regis
February 5, 2018, 8:52pm
5
This should be
{{ with .Resources.GetMatch "header" }}
{{ $cover := . }}
But then you lose .Site and .Scratch
Using your first example:
{{ with .Resources }}
{{ $cover := .GetMatch "header" }}
For both cases you cannot use .Scratch and .Site as is from within your with
block. You will have to store them in variables beforehand.
Thank you for your feedback @regis !
I was still getting a few hundred errors, but at least the images are showing up. Some where in fact because the script I used placed a few empty src
parameters.
In the end, I used your suggestion of with .Resources.GetMatch "header"
Here’s the final, working, code:
{{ with .Resources.GetMatch "header" }}
{{ $cover := . }}
{{ $image := $cover.Resize "500x" }}
<img src="{{ with $image }}{{ $image.RelPermalink }}{{ else }}{{ .Site.BaseURL }}images/fractal_thumb.png{{ end }}" alt="{{ $image.Name }}" />
{{ else }}
<img src="{{ .Site.BaseURL }}images/fractal_thumb.png" alt="header" />
{{ end }}
regis
February 5, 2018, 10:13pm
7
Glad I could help!
Watch out for .Site
though, I’m pretty sure it shoots blanks (pardon my french).
1 Like
I’m already seeing that happen, content is already outputting resources like it should but I can’t get anything from a sections’ _index.md file. Still, with over 2000 files in the content folder, it was not a bad victory
bep
February 5, 2018, 11:08pm
9
While the above is correct, I prefer to use this syntax with “with”:
<img src="{{ with $image }}{{ .RelPermalink }}{{ else }}{{ .Site.BaseURL }}images/fractal_thumb.png{{ end }}" alt="{{ $image.Name }}" />
It does give the code a bit more readability. One last question, if I may, why isn’t the scratch below working?
I used it with params in the past. Is this due to the variable scope in the if clause?
{{- $.Scratch.Add "index" slice -}}
{{- range where .Site.Pages ".Params.unlisted" "!=" true -}}
{{ if .Resources.GetMatch "header" }}
{{- $thumbnail := .Resources.GetMatch "header" -}}
{{- $.Scratch.Add "index" (dict "title" .Title "subtitle" .Params.subtitle "ref" .Permalink "tags" .Params.tags "content" .Plain "image" $thumbnail.Permalink ) -}}
{{else}}
{{- $.Scratch.Add "index" (dict "title" .Title "subtitle" .Params.subtitle "ref" .Permalink "tags" .Params.tags "content" .Plain "image" "https://brunoamaral.eu/images/fractal_thumb.png" ) -}}
{{- end -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}
just posting the final code in case someone anyone has the same issue:
{{- $.Scratch.Add "index" slice -}}
{{- range where .Site.Pages ".Params.unlisted" "!=" true -}}
{{- $image := .Resources.GetMatch "header" -}}
{{- if $image -}}
{{- $thumbnail := $image.Permalink -}}
{{- $.Scratch.Add "index" (dict "title" .Title "subtitle" .Params.subtitle "ref" .Permalink "tags" .Params.tags "content" .Plain "image" $thumbnail) -}}
{{- else -}}
{{- $.Scratch.Add "index" (dict "title" .Title "subtitle" .Params.subtitle "ref" .Permalink "tags" .Params.tags "content" .Plain "image" "https://brunoamaral.eu/images/fractal_thumb.png" ) -}}
{{- end -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}
Thank you @regis !
1 Like