Adding custom functions

Hi,

how can I add a custom function to be used in a template, are there some best practices? actually, I’m not sure if it is possible.

The idea is to be able to use a custom go function to generate some of the content of a given page (that cannot be done with the provided template functions of course). I will appreciate some directions or what to look for (not very familiar with go nor hugo, but haven’t been able to find documentation about it).

thanks!

g

2 Likes

If you fork Hugo and you know Go, then you could write your own template functions and compile your own custom Hugo binary.

@pavel Shortcodes do not provide a Hugo user with the ability to use custom Go Templates functions.

Thanks for the replies. I guess it isn’t as easy as I though. I also had the impression that shortcodes allow the same functionality as templates (only the provided template functions).

best,

g

1 Like

You can use any of the provided Hugo functions in shortcodes.

Reviving this topic, as it hasn’t been specifically addressed.

Adding custom template functions, like range, without the need for forking and compiling Hugo, is something that would simplify our lives as developers.

I see the issue discussed long ago here but without a solution.

I’d help with building functionality if it was thought to be possible.
Thanks!

1 Like

I was just googling how to do this, I think it would be a handy feature and cleaner than very messy partials. I recall using HandleBars and you could hook your own function library which wont be quite as easy in Go (I imagine), and I guess it would be weird to hook and execute Javascript, but I would not be against this.

This has been discussed in several places, sporadically, but I’m not sure that anyone has yet articulated a specific and compelling[1] use case.

Having said that, there are a few native Go packages that might fit:


  1. Favorable ROI compared to other opportunities, no other way to do it, urgency, more than a handful of people requesting it, etc. ↩︎