Detect empty html template type (.TableOfContents)

I’d like a conditional block that depends on .TableOfContents having anything in it.

printf "%#v" .TableOfContents

gives me ""

But when I run

{{If ne .TableOfContents ""}}

it doesn’t work

Also

{{if gt .TableOfContents.length 0}}

gives me:

can't evaluate field length in type template.HTML

Can I cast it to a proper string first? What methods does template.HTML allow? I can’t seem to find any docs about that.

Try

{{If ne (print .TableOfContents) ""}}
1 Like

Perfect. Thanks!

@bep, Why print function is still out in the documentation? Perhaps there are other methods for converting the variable into a string?

Not sure what you mean. Print is part of the standards delivered by Go templates – we have links to that documentation, it is there, print and printf. I have looked. We cannot document every external library we use.

And please don’t mention my name for trivial and general stuff that is not directed at me. It will end up as the boy who cried wolf.

In Hugo 0.16 there is a string func … and a int func.

Sorry, I did not expect that disturb you online.