# Possible to count shortcodes in layout template?

**URL:** <https://discourse.gohugo.io/t/possible-to-count-shortcodes-in-layout-template/29177>\
**Category:** support\
**Created:** [November 1, 2020, 12:01pm UTC](https://discourse.gohugo.io/t/possible-to-count-shortcodes-in-layout-template/29177 "2020-11-01T12:01:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hrishikesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hrishikesh/32/19176_2.png) [@Hrishikesh](https://discourse.gohugo.io/u/Hrishikesh)\
**Post date:** [November 1, 2020, 12:01pm UTC](https://discourse.gohugo.io/t/possible-to-count-shortcodes-in-layout-template/29177/1 "2020-11-01T12:01:14Z")

</div>

I was wondering, if it’s possible to count the number of time a shortcode has appeared in a content file and possibly access its parameter value?

---

<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:** [November 2, 2020, 4:11am UTC](https://discourse.gohugo.io/t/possible-to-count-shortcodes-in-layout-template/29177/2 "2020-11-02T04:11:24Z")

</div>

Only via `.Scratch`.

---

<div class="post-metadata">

**Author:** ![Hrishikesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hrishikesh/32/19176_2.png) [@Hrishikesh](https://discourse.gohugo.io/u/Hrishikesh)\
**Post date:** [November 2, 2020, 6:50am UTC](https://discourse.gohugo.io/t/possible-to-count-shortcodes-in-layout-template/29177/3 "2020-11-02T06:50:31Z")

</div>

How exactly? Something like adding a value to `.Scratch` with every 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:** [November 2, 2020, 8:42am UTC](https://discourse.gohugo.io/t/possible-to-count-shortcodes-in-layout-template/29177/4 "2020-11-02T08:42:05Z")

</div>

I asked the same question and forgot `.Ordinal` already: There is a variable called `.Ordinal` in each shortcode that seems to increments on subsequent calls.

> [@"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…

I would, however, do it like this with scratch (untested, might contain typoes):

```auto
{{ with .Page.Scratch.Get "counter" }}
  {{ .Page.Scratch.Set "counter" (add . 1) }}
{{ else }}
  {{/* first call, so counter is 1 */}}
  {{ .Page.Scratch.Set "counter" 1 }}
{{ end }}
{{ $counter := .Page.Scratch.Get "counter"}}

```

Note that `.Scratch` inside the shortcode stays _inside_ the shortcode. You need to call `.Page.Scratch` to catch the “global” scratch variable.

I am not sure if that `with` part will throw any error if the variable does not exist (first call). It shouldn’t…

---

<div class="post-metadata">

**Author:** ![Hrishikesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hrishikesh/32/19176_2.png) [@Hrishikesh](https://discourse.gohugo.io/u/Hrishikesh)\
**Post date:** [November 2, 2020, 9:47am UTC](https://discourse.gohugo.io/t/possible-to-count-shortcodes-in-layout-template/29177/5 "2020-11-02T09:47:33Z")

</div>

I’m sure I am doing it wrong as I can’t seem to get it working. Here’s what I’m doing:

I created a test shortcode (say, `abc`) and added this to it:

```auto
{{ with .Page.Scratch.Get "counter" }}
  {{ .Page.Scratch.Set "counter" (add . 1) }}
{{ else }}
  {{/* first call, so counter is 1 */}}
  {{ .Page.Scratch.Set "counter" 1 }}
{{ end }}

```

And then just used the shortcode multiple times in one of my content pages.

Then in my `index.html`, I tried this:

```auto
{{ range where .Site.RegularPages "Section" "projects" }}

      {{ $counter := .Page.Scratch.Get "counter"}}

      {{ $counter }}

{{ end }}

```

Sadly, I don’t get anything rendered there. Can this not be used in this way?

If not this, can I somehow use `.Ordinal`? I tried using something like `{{ .Scratch.Set "counter" .Ordinal }}`, but that returned nothing too.

Here’s an unpolished repo: [https://github.com/Hrishikesh-K/Portfolio/tree/v2](https://github.com/Hrishikesh-K/Portfolio/tree/v2)

---

<div class="post-metadata">

**Author:** ![Hrishikesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hrishikesh/32/19176_2.png) [@Hrishikesh](https://discourse.gohugo.io/u/Hrishikesh)\
**Post date:** [November 2, 2020, 2:14pm UTC](https://discourse.gohugo.io/t/possible-to-count-shortcodes-in-layout-template/29177/6 "2020-11-02T14:14:52Z")

</div>

UPDATE: Don’t mind my previous post. I just didn’t try to stop and restart the server. Now when I did it, it works.

I just had to change `{{ .Page...` to `{{ $.Page...` in my shortcode.

EDIT: It’s kinda not working properly. In the sense, it shows the count only when I restart the server, then, if I make any changes to that code, it doesn’t work till I restart the server with the same code that I had before editing. Yes, it doesn’t even work if I add the same code that was working without restarting the server.

---

<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:** [November 3, 2020, 1:32am UTC](https://discourse.gohugo.io/t/possible-to-count-shortcodes-in-layout-template/29177/7 "2020-11-03T01:32:05Z")

</div>

> [@Hrishikesh](#):
>
> EDIT: It’s kinda not working properly. In the sense, it shows the count only when I restart the server, then, if I make any changes to that code, it doesn’t work till I restart the server with the same code that I had before editing. Yes, it doesn’t even work if I add the same code that was working without restarting the server.

I think that is because `hugo server` does only recreate the pages that you changed if you not explicitly run it with that CLI parameter that recalculates everything on changes. I changed my `hugo server` call to be changed behind the scenes to `hugo server --disableFastRender --i18n-warnings --navigateToChanged --templateMetrics --templateMetricsHints --verbose --verboseLog --path-warnings --buildDrafts --buildExpired --buildFuture --watch --enableGitInfo --forceSyncStatic --log true --logFile hugo.log --verbose`. Not sure, but `--disableFastRender` should be that parameter.

---

<div class="post-metadata">

**Author:** ![Hrishikesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hrishikesh/32/19176_2.png) [@Hrishikesh](https://discourse.gohugo.io/u/Hrishikesh)\
**Post date:** [November 3, 2020, 5:35am UTC](https://discourse.gohugo.io/t/possible-to-count-shortcodes-in-layout-template/29177/8 "2020-11-03T05:35:00Z")

</div>

Yeah, I was aware about the `disableFastRender` thing, just wanted to make sure it’s not something I’m doing wrong. Thanks a lot for the help!

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [November 5, 2020, 5:35am UTC](https://discourse.gohugo.io/t/possible-to-count-shortcodes-in-layout-template/29177/9 "2020-11-05T05:35:06Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
