Hi,
i need help in accessing images within a single page.
the file structure is the following:
content
blog
blogentry
mypost.md
images
image1.jpg
image2.jpg
in the mypost.md file i use blocks and template to be able repeat a certain block several times within a blog-post. the file looks like this (simplified)
title: "My awesome blogpost"
draft: false
blocks:
- template: block-gameinfo
title: "Something"
copy: "some Text"
- template: block-youtube
link: test
tile: "some title"
description: "some description"
- template: block-gallery
images:
- image_url : images/image1.jpg
image_alt : "some alt text"
- image_url : images/image2.jpg
image_alt : "some alt text"
- image_url : images/image3.jpg
image_alt : "some other alt text"
now i want to display all images, so that i can put them in a caroussel
{{ define "main" }}
{{.Title}}
{{ range .Params.blocks }}
{{ if eq .template "block-gameinfo" }}
Template Name : GameInfo
{{ else if eq .template "block-youtube"}}
Template Name : YouTube
{{ else if eq .template "block-gallery"}}
Template Name : Gallery <br/>
{{ range .images }}
{{ with .Resources.GetMatch .image_url}}
{{ $resized := .Resize "200x"}}
<img src="{{$resized.Permalink}}" alt="{{ $mainimage_alt }}" />
{{ end }}
{{ .image_url}} <br>
{{ .image_alt }}<br/>
<br/>
{{ end }}
{{ else }}
No Special Template {{ .template }}
{{ end }}
<hr>
{{ end }}
I can access .image_url - but the src in the img-tag will always be emplty. I guess i am missing something obvious here.
{{ .image_url }} and {{ .image_alt }} are there:
images/image1.jpg
some alt text
I see the issue and does not know how to solve it.
The problem I have, i can have several blocks block-gallery,
if i use the following code in block-gallery:
{{ range .Params.blocks }}
{{ range .images }}
{{ $alt_txt := .image_alt }}
{{ with $.Resources.GetMatch .image_url}}
{{ $resized := .Resize "200x"}}
<img src="{{$resized.Permalink}}" alt="{{ $alt_txt }}" />
{{ end }}
{{ end }}
{{ end }}
It works, BUT i have all images everytime.If the block is used twice with 3 images each, i will have both times 6 images.
Therefore I need to give the partial the scope of . , but if i do this, the $.Resources.GetMatch does not deliver the image (the variable is still set, but as i have the wrong scope it does not work as expected).
Is there a solution to this?
Had to change my .md file, each block which can be repeated got a counter, where i have to make sure, that it is unique.
- template: block-gallery
gallery_counter: 1
images:
- image_url : images/image1.jpg
image_alt : "some alt text"
- image_url : images/image2.jpg
image_alt : "some alt text"
- image_url : images/needforspeed_logo.jpg
image_alt : "some other alt text"
- template: block-qoute
qoute: "You can do it"
qouted_person: "Bacon L'Orange"
- template: block-gallery
gallery_counter: 2
images:
- image_url : images/needforspeed_logo.jpg
image_alt : "some other alt text"
- image_url : images/image1.jpg
image_alt : "some alt text"
- image_url : images/image2.jpg
image_alt : "some alt text"
---
I adapted my single.html file
{{ $gallery_counter := 0}}
{{ range .Params.blocks }}
{{ if eq .template "block-gameinfo" }}
{{- partial "block-gameinfo" . -}}
{{ else if eq .template "block-youtube"}}
{{- partial "block-youtube.html" . -}}
{{ else if eq .template "block-qoute"}}
{{- partial "block-qoute" . -}}
{{ else if eq .template "block-content"}}
{{- partial "block-content" . -}}
{{ else if eq .template "block-gallery"}}
{{ $gallery_counter = add $gallery_counter 1}}
{{ $.Scratch.Set "gallery_counter" $gallery_counter}}
{{ partial "block-gallery" $.Page }}
{{ else }}
No Special Template {{ .template }}
{{ end }}
{{ end }}
I added here a $gallery_counter and in the gallery-tempalte part I add 1 and set it as a Scratch.
In my block-gallery.html tempalte I can now get the variable and check, that it has the right value. That way I get the correct block and just my images I wand in this gallery.
{{ $gallery_counter := .Scratch.Get "gallery_counter"}}
{{ range .Params.blocks }}
{{ if eq .gallery_counter $gallery_counter}}
{{ range .images }}
{{ $alt_txt := .image_alt }}
{{ with $.Resources.GetMatch .image_url}}
{{ $resized := .Resize "200x"}}
<img src="{{$resized.Permalink}}" alt="{{ $alt_txt }}" />
{{ end }}
{{ end }}
{{ end }}
{{ end }}
Probably not the best solution, but one I can live with right now - or have to live with.
But if someone has an idea for a nicer and cleaner solution (without the additional counter in the .md file), this would be nice.
Maybe I have this idea tomorrow., i would then reply here again