Trigger event if shortcode is used

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 }}

A little bit hard to read from your stripped sample …

But I have done similar with …

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 }}
  1. My gallery use case is different. I really need the per-post gallery id
  2. The scope of a given Scratch is limited to the given Page or Node, it’s not global (but I guess it’s global enough)