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
This commit is contained in:
Martin Rubli
2025-04-15 09:29:48 +02:00
parent 3fb5f2015b
commit 1c8bce4a66

View File

@@ -9,10 +9,12 @@
{{ $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 .Get "apiKey" }}
{{ with $apiKey }}
apiKey: '{{ . }}',
{{ end }}
};