# Statically initialize a shortcode?

**URL:** <https://discourse.gohugo.io/t/statically-initialize-a-shortcode/17202>\
**Category:** support\
**Created:** [February 25, 2019, 12:51am UTC](https://discourse.gohugo.io/t/statically-initialize-a-shortcode/17202 "2019-02-25T00:51:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![atraver](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@atraver](https://discourse.gohugo.io/u/atraver)\
**Post date:** [February 25, 2019, 12:51am UTC](https://discourse.gohugo.io/t/statically-initialize-a-shortcode/17202/1 "2019-02-25T00:51:51Z")

</div>

I’m looking for a way to statically initialize a shortcode. What I mean by that is, take for example an image gallery shortcode. This shortcode might show up multiple times on any given page, but it uses a shared JS library that should only be included/initialized once per page.

```
{{< gallery >}}
  {{< img src="one.jpg" >}}
  {{< img src="two.jpg" >}}
{{< /gallery >}}

```

Inside the `gallery.html` shortcode file, I’d like to have some way to tell my global theme to include the required JS file, ideally in the `baseof.html` file. Something like (this is contrived but just to get an idea):

```
<section class="gallery">
  {{.Inner}}
</section>

{{ define "one-time-scripts" }}
  <script src="/js/siema.min.js">
  <script>
    new Siema();
  </script>
{{ end }}

```

I only use `define` in the example because it _seems_ like `blocks` are the way to go here, I’m just not sure how to get them to place nicely together.

Importantly:

- Including multiple `gallery` shortcodes on a single page should _not_ add multiple `<script>` tags to the page itself; and
- the `one-time-scripts` block should append to whatever the theme has already been provided in that block, be it through theme partials or other shortcodes, but only once in the case of the `gallery` shortcode (append, just not multiple times).

Is this possible?

---

<div class="post-metadata">

**Author:** ![pointyfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pointyfar/32/5797_2.png) [@pointyfar](https://discourse.gohugo.io/u/pointyfar)\
**Post date:** [February 25, 2019, 1:11am UTC](https://discourse.gohugo.io/t/statically-initialize-a-shortcode/17202/2 "2019-02-25T01:11:16Z")

</div>

> [@atraver](#):
>
> This shortcode might show up multiple times on any given page, but it uses a shared JS library that should only be included/initialized once per page.

Hi,

You could use [`.HasShortcode`](https://gohugo.io/templates/shortcode-templates/#checking-for-existence) to check for the existence of a shortcode.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [February 25, 2019, 5:01am UTC](https://discourse.gohugo.io/t/statically-initialize-a-shortcode/17202/3 "2019-02-25T05:01:47Z")

</div>

I had the same issue once and if you are using multiple different shortcodes that won’t need the script you might want to look into `.Ordinal` or the solution by @brunoamaral.

> [@"Static Variable" that increments through shortcodes?](https://discourse.gohugo.io/t/static-variable-that-increments-through-shortcodes/14120):
>
> Hi all, is there some kind of static variable system in the new Hugo version that I could use to create a Bootstrap slider shortcode system? I would want to register a counter in the outer shortcode that receives an increment per child-shortcode and can be used later AFTER the {{ .Inner }} is processed in the outer shortcode.strong text Long version: The final HTML should be: \<div id="carousel" class="carousel slide carousel-fade" data-ride="carousel"\> \<div class="carousel-inner"\> \<div…

---

<div class="post-metadata">

**Author:** ![atraver](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@atraver](https://discourse.gohugo.io/u/atraver)\
**Post date:** [February 25, 2019, 8:09am UTC](https://discourse.gohugo.io/t/statically-initialize-a-shortcode/17202/4 "2019-02-25T08:09:44Z")

</div>

Thanks for the great answers!

I think I’m going to go the route of `.HasShortcode` for now, unless I can figure out something even cleaner. It would be ideal if I could encapsulate the entirety of the functionality in the shortcode’s HTML file itself (as opposed to specifying the JS in the footer partial), but it’s not _that_ big of a deal.

---

<div class="post-metadata">

**Author:** ![ju52](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ju52/32/6749_2.png) [@ju52](https://discourse.gohugo.io/u/ju52)\
**Post date:** [February 26, 2019, 8:36am UTC](https://discourse.gohugo.io/t/statically-initialize-a-shortcode/17202/5 "2019-02-26T08:36:10Z")

</div>

> **[.Scratch](https://gohugo.io/functions/scratch/)**
>
> Acts as a "scratchpad" to allow for writable page- or shortcode-scoped variables.
