If .PathMatches "/"

(Flav Allaz) #1

Hi everybody,

I’m trying to use the .PathMatches template action in order to know if the user is on the homepage. I tried:

{{if .PathMatches “/”}}

But then this statement get true on every page (eg. “/xyz.html”). Is it normal? What can I use instead?


(Matthew Fay) #2

Hi @flav-allaz,

The .PathMatches function exposes the Path RequestMatcher to the template context. This matcher compares the request URI against a base path, rather than an exact match, and it’s frequently used to determine if a middleware configuration is applicable to a given request. For this purpose, a path of / will match every request without fail.

You could instead check the exact path by using the built-in eq function and the URL.Path variable:

{{ if eq .URL.Path "/" }}