Updating Taxonomies using Shortcodes

I was feeling really happy with some shortcodes I wrote for defining terms and creating popups.

I wondered if I could automatically add the term to a taxonomy, which meant take a parameter passed to a shortcode and add it to .Page.Params.tags. I tried:

{{ $term := .Get 0 }}<br/>
{{ $newtag := slice ( $term ) }}
{{ if isset .Page.Params "tags" }}
    {{ $tags := .Page.Params.tags }}
    {{ $tags = $tags | append $newtag }}
    <br/>$tags: {{ $tags }}
{{ end }}
<br/>TAGS: {{ .Page.Params.tags }}

When this runs, I see the new term (parameter 0 to the shortcode) along with hardcoded front matter in the “tags” variable on the HTML page after $tags.

If I include {{ .Page.Params.tags := $tags }} before the TAGS: line above, I get an error: defdef.html:7:1": parse failed unexpected “:=” in operand

If I include {{ .Page.Params.tags = $tags }} before the TAGS: line above, I get an error: defdef.html:7:1": parse failed unexpected “=” in operand

It seems there is no way to update the .Page.Params.tags variable. Am I trying to attempt the impossible here?

Yes, you are.

Thanks, I wouldn’t be the first time! :grinning:

Would it be useful though if it were possible? I personally despite having to try to keep stuff in-sync between page params and page content. I think the ability for shortcodes to influence the page’s params would be incredibly powerful!

Think about it for a bit. Hugo renders templates in parallel and caches results.

So I haven’t looked at the Hugo code, so I’m speculating here, but…

I assume it reads the config.toml first, so it knows what the taxonomies are and it can’t build them before it has at least read the front matter from all the other pages. The rendering could be done in two phases, right? Non-taxonomy (which could update front matter, or even taxonomies directly with the right functions) and then taxonomy pages.

Thank you for engaging me here, I really appreciate your feedback! :slight_smile: