Custom Functions

Hi Folks,

Newb user here. I’ve seen some hints here and there throughout the forum but never anything definative.

What I’d like to do is build a custom library of routines and functions that I can use.

For example I might want to take the date that I create a page (say 2025-05-15) convert it directly to a “number” (20250515) and then convert that from base 10 to base 36 (c21eb)

Such as you can do here:

Is this something I could conceivably do as a shortcode or module?

Or would this require me to fork and compile?

Thanks

To do what?

Hello, it seems to me that such calculators are easier to make in JavaScript, which can then be embedded into a separate template for a specific Hugo page (since it won’t be used anywhere else except there).

1 Like

Thanks for getting back

Well for example the calculator above. I actually use that to generate internal links on some websites (anchors within a page). Only now I have to do the calcs manually.

So for me if this is something that I could build into my HUGO processing great - a custom shortcode or function I can call at “compile” time.

rather than an external tool I’d have to build.

That would work if I wanted it to be dynamic on the pager rather than a static artifact..

Plus - on this latest project I’m looking to see if I can eliminate JavaScript on everything except form processing… :wink:

Thanks for the idea though. I hadn’t thought of that. Would work on another project of mine.

The Hugo equivalent of a custom function is a partial template that returns a value instead of rendering something. Within the partial template you can call any of Hugo’s built-in functions and methods.

Note the only relevant encoding function is for base 64, but one of the non-crypographic hash functions might be useful as well. Of particular note is the xxHash function which is really fast.

We add new built-in functions from time to time. If there are one or two things in particular that we’re missing, and that are simple to implement, document, and support, make a suggestion.

Otherwise you will have to fork.

Have you tried defining a custom output format? Seems like you could define or override based on a specific page | path