Using strings.Repeat

I am trying to use the strings.Repeat function in a partial template. While I can use the other string functions such as strings.TrimPrefix, I cannot get strings.Repeat to work. I consistently get the error:

error calling partial: template: partials/test.html:4:10: executing "partials/test.html" at <strings>: can't evaluate field Repeat in type interface {}

The testing partial template is:

 <pre>
{{ print "begin" }}
{{ print (strings.TrimPrefix "ab" "abba" ) }}
{{ print (strings.Repeat "yo" 4) }}
{{ print "end" }}
</pre>

Instead of the error I had expected to see:

begin
ba
yoyoyoyo
end

The partial template works to without the strings.Repeat call to produce:

begin
ba
end

Am I doing something wrong? Or could it be improperly implemented or documented?

I noticed in the code for strings.Repeat that the order of arguments are reversed compared to the docs. EG: the docs for strings.TrimPrefix and code are both prefix string, where as one Repeat is input count and the other is count input

{{ “yo” | strings.Repeat 4 }}

1 Like

Thanks for the reply bep!

Changing the template to:

<pre>
{{ print "begin" }}
{{ print (strings.TrimPrefix "ab" "abba" ) }}
{{ "yo" | strings.Repeat 4 }}
{{ print "end" }}
</pre>

does not stop the error from occurring. I still get

error calling partial: template: partials/test.html:4:10: executing "partials/test.html" at <strings>: can't evaluate field Repeat in type interface {}

What is the output of hugo version?

hugo version gives me:

Hugo Static Site Generator v0.38-DEV linux/amd64 BuildDate:

I’ll try updating and see if that fixes it.

strings.Repeat was introduced in Hugo 0.42

That clears it up. Thanks fellows!

looks like the documentation is wrong? The version of @bep works but the doc’s version gives “hugo unable to cast of type string to int”. Anyway I am delighted with the answer above :slight_smile:

Submitted PR to fix docs:
https://github.com/gohugoio/hugoDocs/pull/1264