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 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).
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!
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.
I use {{ T “i18n-key” }} to translate many strings throughout my multi-lingual site. Translators must copy and update the en-us.yaml file with all of its many entries. It is not reasonable to ask them to create multiple lines for simple string casing (if that would even work; might be case-insensitive, haven’t checked):
mystring: my string
Mystring: My string
MYSTRING: MY STRING
And I don’t want to little my partials with {{ T “mystring” | strings.FirstUpper }}. While it is readable and clear what is happening, I would prefer to extend “T” as “TT” (for example), so that the above becomes simple {{ TT “mystring” }} → “My string”.
This should be interesting for the Hugo ecosystem as well, since some really useful go extensions will naturally be built, and could be siphoned into core code when their value add is compelling enough.