"Static Variable" that increments through shortcodes?

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 class="carousel-item active">
      <img class="d-block w-100" src="image.jpg" alt="Third slide">
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src="image2.jpg" alt="Second slide">
    </div>
    ...
  </div>
  <ol class="carousel-indicators">
    <li data-target="#carousel" data-slide-to="0" class="active"></li>
    <li data-target="#carousel" data-slide-to="1"></li>
    ...
  </ol>
  <a class="carousel-control-prev" href="#carousel" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carousel" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

In shortcode I would assume:

{{< slideshow parameter="" parameter2="" >}}
{{< slideshowimage url="blabla" >}}
{{< slideshowimage url="blabla" >}}
{{< /slideshow >}}

The .Inner tag would count up a variable per occurance of slideshowimage that I could use for the #carousel-indicators section which has x items - same amount as the images in the shortcode.

did you try scratch? (I never used it in shortcodes though)

The default Shortcode’s .Scratch is unique to the shortcode, so you’d have to use the page’s .Scratch from within the shortcode aka .Page.Scratch.

I don’t see why it would not work…

1 Like

There is a .Ordinal available on shortcode which should fit your use case.

2 Likes

I did something similar using the page resources and :conter as a string: Adding a photoswipe gallery as a shortcode using Page.Resources

1 Like