There might be an obvious way to do this, but I’m trying to get all of the .Resources in a leaf bundle but it’s a mixture of images and markdown files with video embeds. I know the syntax below is slightly off, but is there an easy way to do something similar to:
{{ $images := .Resources.ByType "image" }}
{{ range .Resources }}
{{ if eq $images }}
<div class="img-container" >
<img src="{{ .Permalink }}" style="width:100%; height:auto;">
</div>
{{ else }}
<p>{{ .Content }}</p>
{{ end }}
{{ end }}
The goal is for us to be able to quickly add images to a folder in order to update our website, but occasionally add a video link and drop it in that folder as well. Ideally everything within a project folder would be flattened, and the single.html page function above would loop through all assets, and could output/render each resource type differently.
Interesting, it does seem like it should work, but I get this error when building:
$ hugo
Building sites …
ERROR 2018/06/21 08:16:55 \
Error while rendering "page" in "projects/centerforhumanities/": template: /Code/pnp-web/layouts/_default/single.html:52:10: \
executing "main" at <eq>: wrong number of args for eq: want 2 got 1
This does grab all of the Resources that are images, but the {{ .Content }} block as it’s written is outside of the loop, therefore returns the content from index.md instead of videos.md
You should test .ResourceType property of your resource object and find what you want in there. If the main type is not enough, you’ll have to test .RelPermalink for now.
You can use strings.Contains for that. I explain the trick here.
Note that there may be a better approach since I experimented with Page Resrouces, but this one will work.
Thanks regis! That worked. Now I’m wondering about making another tweak. Does .Resources prioritize / pull in .jpg file types first and .md pages second? I tried renaming the files to:
01-abm.jpg
02-abm-video.md
...
etc
but the videos/markdown resources still get pulled in last. Is that how it will always work? Any way to adjust that? (I think I could probably add a parameter for the order, but I’d prefer it to be ordered alphanumerically by filename.
Below is what I ended up with on single.html and you can see the site in development here.
{{ range .Resources }}
{{ if (strings.Contains .RelPermalink ".jpg") }}
<div class="img-container" >
<img src="{{ .Permalink }}" style="width:100%; height:auto;">
</div>
{{ else if (strings.Contains .RelPermalink ".pdf") }}
<p>PDF Download: {{ .RelPermalink}}</p>
{{ else }}
{{ .Content }}
{{ end }}
{{ end }}