The value in .RelPermalink and the return value from relURL are not comparable?

I have a site with setting baseurl = "company.com/xyz

in my home template, I have the below code, which works great:

 {{ range sort (where .Site.Pages "Section" "index" ) }}
        {{ if eq .RelPermalink "/xyz/index/hero/" }}
            {{ partial "hero.html" . }}
        {{ end }}
       ....
{{ end }}

However, consider the xyz in the url may change over time, so I decided to make it a bit dynamic in my code, which come out the blow code. The code didn’t work, the if clause always evalute to false, even though I’m sure the {{ "/index/hero/" | relURL}} and ‘.RelPermalink’ are all the same value: “/xyz/index/hero/”

{{ range sort (where .Site.Pages "Section" "index" ) }}
        {{ if eq .RelPermalink ("/index/hero/" | relURL)}}
            {{ partial "hero.html" . }}
        {{ end }}
       ....
{{ end }}

Please kindly advice what could be the problem? Thank you very much

As I understand it: if they have the same value, they don’t have to be the same type. For instance, you might be comparing a string (the hardcoded "/xyz/index/hero/") with an URL (.RelPermalink). Even though both might have the same value, they’re not the same in the sense that they’re of different types.

If I understand that correctly, you can try the string function to convert .RelPermalink to a string.

thank you very much for the reply. totally a newbie here. When I show the values on the page, they are exactly the same. Is there anyway I can log the ‘raw value’ of the two mentioned above, so that I can understand why they are evaluated as NOT EQUAL. Thank you very much in advance.

I would try something like this:

{{ if eq (.RelPermalink | string) ("/index/hero/" | relURL | string)}}
    {{ partial "hero.html" . }}
{{ end }}

If converting to strings helps, you can then make the code clearer by only converting the part of the if statement that actually needs to be converted to string. (I can’t test right know to see which, if any, of your two expressions are strings.)

Awesome, Thanks a million @Jura . When I did the convert /index/hero/" | relURL | string, everything works like charm.

Just dig a little deeper and found out, the return value of RelPermalink is string, whilst the return value of relURL is template.HTML

func (p *Page) RelPermalink() string {
	return p.relPermalink
}
func (ns *Namespace) RelURL(a interface{}) (template.HTML, error) {
	s, err := cast.ToStringE(a)
	if err != nil {
		return "", nil
	}

	return template.HTML(ns.deps.PathSpec.RelURL(s, false)), nil
}
1 Like