Files
hugo-mod-leaflet/layouts/shortcodes/leaflet-layer.html
Martin Rubli f3746e0fc4 layouts: map: Make unique ID unique on page, not only shortcut parent
The SHORTCODE.Ordinal method "returns the zero-based ordinal of the shortcode in
relation to its parent." (https://gohugo.io/methods/shortcode/ordinal/)

This is a problem if the same shortcode is used on the different page through
different paths, e.g. once on the page itself and once by a partial (e.g. a
header or footer).

Instead of just using the .Ordinal by itself, use a hash of the parent file
_and_ the ordinal, which gives us a properly unique identifier.
2023-12-18 23:35:44 +01:00

23 lines
679 B
HTML

{{ if or (not .Parent) (not (eq .Parent.Name "leaflet-map")) }}
{{ errorf "%s: cannot be used outside leaflet-map: %s" .Name .Position }}
{{ end }}
{{ if not (isset .Params "id") }}
{{ errorf "%s: missing 'id' parameter: %s" .Name .Position }}
{{ end }}
{{ $uniqueMapId := (printf "%s:%d" .Page.File.UniqueID .Parent.Ordinal) | md5 | safeJS }}
{{ $id := .Get "id" }}
<script>
map_options_{{ $uniqueMapId }}.layers.enabled.push({{ $id }});
map_options_{{ $uniqueMapId }}.layers["{{ $id }}"] = {
{{ with .Get "apiKey" }}
apiKey: '{{ . }}',
{{ end }}
};
{{ with .Get "selectorPosition" }}
map_options_{{ $uniqueMapId }}.layers.position = {{ . }};
{{ end }}
</script>