I would like to create a shortcode for a theme that allows an author to include an image gallery into an article. Since this feature requires the load of dependencies like jQuery and the image gallery plugin itself, I would like to include those only into the page if the short code was used.
I thought about somethink like this (but the ‘trigger’ is still missing):
# Somewehre in an article
{{< myGalleryShortcode >}} {{< /myGalleryShortcode >}}
# ...
# In the footer
{{ if galleryIncluded }}
# load dependencies
{{ end }}
In the repo of your blog bepsays.is I saw you define a gallery-id in the frontmatter and include the corresponding partial and the assets.
However, with Scratch I could set a global variable like useGallery to true (with $.Scratch.Set "useGallery" true) and include the assets based on the boolean, like here:
{{ if $.Scratch.Get "useGallery" }}
# include the assets
# Set useGallery to false so that other posts don't include the assets
{{ $.Scratch.Set "useGallery" false }}
{{ end }}