I have a working shortcode for an images gallery as follows:
{{ with $.Get "title" }}<h3>{{ . }}</h3>{{ end }}
{{ with $.Get "content"}}<h4>{{ . | markdownify }}</h4>{{ end }}
<div class="gallery">
{{ range $image := (.Page.Resources.ByType "image").Match (printf "%s*" (.Get "src")) }}
{{ $src := $image }}
{{ $thumb := .Resize "500x" }}
<a data-fslightbox="gallery" href="{{ $src.RelPermalink }}">
<img
class="lazyload blur-up"
data-src="{{ $thumb.RelPermalink }}"
alt="{{ .Name | markdownify }}"
>
</a>
{{ end }}
</div>
This shortcode works well when I have a single shortcode in the same page (display the image gallery and showing each single large format image when clicking on thumb).
But the same process is not working when I get 2 or 3 similar shortcodes in the same page.The 3 images galleries are displaying as expected but not the single large format when clicking on the thumb.
Note that I have a conditional call of a light js script whith single layout as follows:
{{- if .HasShortcode "gallery" -}}
{{ $fslightbox := resources.Get "js/fslightbox.js" | fingerprint -}}
<script defer src="{{ $fslightbox.RelPermalink }}"></script>
{{ $gallery := resources.Get "css/gallery.css" | minify | fingerprint }}
<link type=text/css rel="stylesheet" href="{{ $gallery.Permalink }}">
{{ end }}
Any idea whether I am missing something or doing something wrong?
Because you are using same name for all the fslightbox instances.
data-fslightbox="gallery"
fslightbox instances documentation
You need generate unique id for each fslightbox instances, you can read a tips shared here:
I recently fixed an issue where I needed to generate a random string. The string had to be random, even if given the same input seed.
In short, there is a shortcode that creates “expandables” with a little js. Each expandable generates its html id based off the md5 hash of the shortcode’s .Inner content. Well, if two expandables had the same inner content, then things would break. My solution was:
Get the md5 hash of the seed string
Split that into an array
Shuffle the array
Convert the array…
you can try this: (not tested)
{{ with $.Get "title" }}<h3>{{ . }}</h3>{{ end }}
{{ with $.Get "content"}}<h4>{{ . | markdownify }}</h4>{{ end }}
+ {{ $seed := .Get "src" }} <!-- you can change this -->
+ {{ $unique := delimit (shuffle (split (md5 $seed) "" )) "" }}
<div class="gallery">
{{ range $image := (.Page.Resources.ByType "image").Match (printf "%s*" (.Get "src")) }}
{{ $src := $image }}
{{ $thumb := .Resize "500x" }}
+ <a data-fslightbox="{{ $unique | safeHTMLAttr }}" href="{{ $src.RelPermalink }}">
<img
class="lazyload blur-up"
data-src="{{ $thumb.RelPermalink }}"
alt="{{ .Name | markdownify }}"
>
</a>
{{ end }}
</div>
Thanks for the swift answer.
I have to investigate that further.
Your proposed code is working well on localhost but not on production site.
I will revert to you as soon as I get the right code.
At the end, I succeeded with your proposed code with minor changes in the way the js script is loaded.
Now it works like a charm.
Once again thanks for the support
1 Like
I’ve followed another route for calculating unique IDs:
{{/* Get address, protocol and other parameters */}}
{{- $address := .Get "address" | default (.Get 0) -}}
{{- $protocol := .Get "protocol" | default "mailto" -}}
{{- $class := .Get "class" -}}
{{- $displaytext := .Get "display" -}}
{{- $parts := split $address "@" -}}
{{- $user := (index $parts 0) -}}
{{- $domain := (index $parts 1) | default "" -}}
{{- $query := .Get "query" | default "" -}}
{{/* Compute md5 fingerprint */}}
{{- $fingerprint := md5 (print $address $protocol (index (seq 999 | shuffle) 0)) | truncate 8 "" -}}
{{/* Set via CSS what is displayed when Javascript is disabled. Query is never displayed */}}
<style type="text/css">
#span-{{ $fingerprint }}.cloaked-e-mail:before {
content:{{ with $domain }}attr(data-domain) "\0040" {{ end }}attr(data-user);
unicode-bidi:bidi-override;
direction:rtl;
}
</style>
 <span class="cloaked-e-mail" data-user="{{ range $index := seq (sub (len $user) 1) 0}}{{ substr $user $index 1}}{{ end }}"{{ with $domain }} data-domain="{{ range $index := seq (sub (len $domain) 1) 0}}{{ substr $domain $index 1}}{{ end }}"{{ end }} id="span-{{ $fingerprint }}"></span> 
{{/* Alter display with Javascript by changing DOM */}}
Thanks for this alternative. Since I am not a pro, I should have some difficulties to explore and understand the details of your solution. However it’s fine to have multiple solutions.
By the way, j’ai vu que votre site était francophone (comme moi) donc ce devrait être plus simple pour moi.
Merci
1 Like
system
Closed
June 27, 2020, 10:06am
8
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.