Files
hugo-mod-leaflet/layouts/shortcodes/leaflet-layer.html
Martin Rubli 1c8bce4a66 layouts: layer: Allow retrieving API key from config
The config key name is the layer name with '.' replaced with '_'. For example:

params:
  modules:
    leaflet:
      com_thunderforest_outdoors:
        apiKey: abcdef1234567890abcdef1234567890

If the key is specified in both the config file and the tag attribute, the
latter takes precedence.

Implements #6
2025-04-15 17:20:13 +02:00

25 lines
817 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" }}
{{ $configId := replace (.Get "id") "." "_" }}
{{ $apiKey := default (index .Site.Params.modules.leaflet $configId "apiKey") (.Get "apiKey") }}
<script>
map_options_{{ $uniqueMapId }}.layers.enabled.push({{ $id }});
map_options_{{ $uniqueMapId }}.layers["{{ $id }}"] = {
{{ with $apiKey }}
apiKey: '{{ . }}',
{{ end }}
};
{{ with .Get "selectorPosition" }}
map_options_{{ $uniqueMapId }}.layers.position = {{ . }};
{{ end }}
</script>