Problems with safeHTML

I have a param in the front matter in my post but I wanted to use html as I need them to be lists <li> but I’m getting an error.

Error while rendering “page” in “work/”: template: work/single.html:19:29: executing “work/single.html” at : wrong number of args for safeHTML: want 1 got 0

On my single I have:

<ul>
{{ .Params.goals | safeHTML }}
</ul>

and on my post front matter:

goals: “<li>One</li><li>Two</li><li>Three</li>

I don’t know what I’m doing wrong, any idea?

We need to see your site’s code, and specifically that single.html template, in order to replicate your error. Check out requesting help, and share a link. :slight_smile:

1 Like

Like this?

it’s weird because it works if I add the safeHTML part while the server is running but when I run hugo is when that error shows up.

If someone has time to reproduce the error from the file you shared, maybe we can figure it out. Good luck! :slight_smile:

1 Like

@ramiroruiz I cannot reproduce this error locally. My guess would be that you’re missing goals: on at least one of your markdown files that’s rendering according to the single.html template.

Please try checking for the existence of the param first and let me know if you’re having the same issue. Modify the code you shared here.

{{with .Params.goals}}
<ul>
    {{ . | safeHTML }}
</ul>
{{end}}

IMHO, this is also more tasteful in that the entire unordered list only renders if the param is available, thereby eliminating the chances of creating an empty <ul> for no reason. HTH.

1 Like

I don’t know why but changing

<ul>
{{ .Params.goals | safeHTML }}
</ul>

with

as you recommended fix the problem. Thank you!

2 Likes

Your original code block requires you to include a .Params.goals in every single content file of your Hugo project.

The {{with .Params.goals}} that @rdwatters posted checks whether that parameter exists first and then calls it.

As you found out calling a parameter from the templates without checking for its existence causes an error if it’s missing from somewhere.

1 Like

Thank you for clearing that up for me, I’ll remember that from now on.

@ramiroruiz To add to what @rdwatters said, you don’t see this failure when running hugo server because you probably have the disableFastRender variable at its default value of false. So it renders only few last modified/related files and not all the .md files you might have. You should be able to see the same error in hugo server too is you have disableFastRender = true in your config.toml.

FWIW I always have that setting in my config.toml to avoid surprises like these, as I don’t have sites large enough to benefit from the fast/partial rendering feature.

2 Likes

Wow :hushed: that’s so useful! thank you, I will add it for sure.