Evaluating .Pre in menu template

I try to add icons to my menu based on the value of the .Pre variable.

Sample (simplified):

config.toml:

[[menu.main]]
name = "Archiv"
weight = 100
identifier = "archiv"
url = "/archiv/"
pre = "archive"

navigation.html

{{ range .Site.Menus.main }}
    {{ if eq .Pre "archive" }}
        {{ partial "icons/archive.svg" . }} 
    {{ end }}
{{ end }}

But it appears that the if-eq-evaluation is returning false (putting parentheses around the eq group does not change it). Why is that?

I verified that the eq query results in false with an {{ else }} rule echoing the contents of .Pre.

By the way: trying to load the partial with a variable as a path is not working either, as in

{{ partial .Pre . }} 

Maybe .Pre is not a string at this point?

The docs here say that it is of type template.HTML.

Perhaps comparing using if eq (print .Pre) ... instead might work?

3 Likes

Good idea. And if that still doesn’t work, you could try the string function

Perfect.

{{ if eq (print .Pre) "home" }}
{{ partial "icons/home.svg" . }} 
{{ end }}

does the trick. It never occured to me to look for type in the documentation :slight_smile:

Now I can even go back to my initial version and put the path into .Pre.

Thanks!