hasPrefix function doesn't work as expected

I noticed that depending on whether a summary was produced automatically or via a <!–more—> break, it can be either plain text or HTML. This means if I embed it as {{ .Summary }} it may not appear inside a paragraph tag, and therefore miss some CSS styling. My idea was to make a case distinction like this:

{{ if hasPrefix .Summary "<" }}
  {{ .Summary }}
{{ else }}
  <p>{{ .Summary }}</p>
{{ end }}

However, I get a template error at the first occurance of .Summary in the line with the hasPrefix function.

I looked at the source code and found that the first parameter of the hasPrefix function does not have a type declaration. Can this be the cause for this problem? Or am I doing something wrong?

See also https://github.com/spf13/hugo/commit/2e92f368907c51cfbb2d56c4519604543e2920f5.

Also, is there a more elegant solution for the above problem?

By the way, as a workaround, I’m currently using this, which works fine:

{{ if eq (substr .Summary 0 1) "<" }}
  {{ .Summary }}
{{ else }}
  <p>{{ .Summary }}</p>
{{ end }}

It would be useful to see that error.

@bep the error message is:

ERROR: 2016/12/22 22:38:52 general.go:236: Error while rendering page : template: theme/index.html:11:16: executing "theme/index.htm…"

OK, I may be a fault for the not so useful error message, we will fix that …

I will guess that you have a datatype problem, summary is template.HTML, hasPrefix expects two strings, so:

{{ if hasPrefix string(.Summary) "<" }}

I guess you mean this, which is working for me:

{{ if hasPrefix (string .Summary) "<" }}

Do you have an explanation why this casting to string is needed with hasPrefix, but not with substr? Can hasPrefix be changed so that it works like substr?

Yes it can.