String manipulation in Hugo

I’m trying to get a somewhat common length of titles (and later excerpts, summaries, etc) for my homepage. The problem is, when I try to use something like this:

{{ slicestr .Title 0 30 }}

If the .Title has less than that number of characters, I get this error:

ERROR: 2015/09/17 Error while rendering homepage: template: theme/index.html:29:46: executing "theme/index.html" at <slicestr .Title 0 50>: error calling slicestr: slice bounds out of range

I figured I would simply do something to count the length of the string, but it doesn’t seem like Hugo has any built-in functions for doing this.

I did find this example, but I’m not quite sure where to put the go functions referenced. I assume they do not go in the template themselves?

Hugo only adds some nice template functions on top of the Go templating language. Try the following code:

{{ if gt (len .Title) 30 }}
    {{ slicestr .Title 0 30 }}
{{ else }}
    {{ .Title }}
{{ end }}

Have a look at substr – my guess that is what you really want; it is more flexible and is very similar to the PHP variant.