When we got [menu]
and its elements specified in hugo.toml
file, we can easely range them and output in spedific template using something like {{ range .Site.Menus.main }}
.
I used simillar approach for custom properties but additionally wanted to limited to whats matched with post frontmatter.
The idea and working example (to best of my knowledge, what I managed to figure out).
[params]
[[params.slide]]
identifier = "drink"
name = "Your fav drink"
picture = "/img/drink.jpg"
path = "https://example1.com/show-me-drinks"
[[params.slide]]
identifier = "meal"
name = "Meals of the Day"
picture = "/img/meal.jpg"
path = "https://example2.com/meals-of-the-day"
[[params.slide]]
identifier = "side"
name = "Sides in a Menu"
picture = "/img/side.jpg"
path = "https://example3.com/menu/sides"
[[params.slide]]
identifier = "dessert"
name = "Places where I can buy desserts"
picture = "/img/dessert.jpg"
path = "https://example4.com/local/places/shop/for/dessert"
Having that specified, I would like to range it and return on page via shortcode.
{{< slides >}}
Content of the shortcut (working)
<div id="slides">
{{ range .Site.Params.slide }}
<div class="slide">
<a href="{{ .path }}">
<img src="{{ .picture | absURL }}" />
<span>{{ .name }}</span>
</a>
</div>
{{ end }}
</div>
This is working. This will list (range) all items from config file. This will be good for one page that contain all, but on some pages I want to display it only based on frontmatter values - limit to identifieds stated like:
---
title: "Recipe for Coconut Drink"
slides:
- drink
- dessert
---
Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum
Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum
{{% slides %}}
Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum
In such way, when there is fontmatter limit in place, only to specified identifiers, it will output only as follow:
<div id="slides">
<div class="slide">
<a href="https://example1.com/show-me-drinks">
<img src="https://example.com/img/drink.jpg" />
<span>Your fav drink</span>
</a>
</div>
<div class="slide">
<a href="https://example4.com/local/places/shop/for/dessert">
<img src="https://example.com/img/dessert.jpg" />
<span>Places where I can buy desserts</span>
</a>
</div>
</div>
I come out with something like that (it may be a bit complex unnecessarly, but its initially working)
<div id="slides">
{{ range .Site.Params.slide }}
{{ $siteIdentifier := .identifier }}
{{ $siteName := .name }}
{{ $sitePicture := .picture | absURL }}
{{ $sitePath := .path }}
{{ range $.Page.Params.slides }}
{{ $pageIdentifier := . }}
{{ if eq $pageIdentifier $siteIdentifier }}
<div class="slide">
<a href="{{ $sitePath }}">
<img src="{{ $sitePicture }}" />
<span>{{ $siteName }}</span>
</a>
</div>
{{ end }}
{{ end }}
{{ end }}
</div>
Any idea to improve, correct final one will be appreciated.
Reference posts: