HUGO

Possible to count shortcodes in layout template?

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?

Only via .Scratch.

How exactly? Something like adding a value to .Scratch with every shortcode?

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.

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

{{ 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…

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:

{{ 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:

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

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.

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.

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!

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