HUGO

Check if the current path in gohugo matches a pattern with regex

I’m trying to check if the current url is of the format : http://localhost:1313/categories/.../ so matching routes under categories route, using regular expression.

I tried this:

{{if (eq .Permalink (`categories/*/` | absURL))}}
    <h4 class="mb-3 text-dark font-weight-bold">
        Showing posts with <mark>{{.Title}}</mark> category
    </h4>
{{else}} # means :{{if (eq .Permalink (`tags/*/` | absURL))}}
    <h4 class="mb-3 text-dark font-weight-bold">
       Showing posts with <mark>{{.Title}}</mark> tag
    </h4>
{{end}}

There’s an easier way to do this.

When you’re visiting http://localhost:1313/tags/ or http://localhost:1313/tags/foo/

Code Value
{{ .Data.Singular }} tag
{{ .Data.Plural }} tags

When you’re visiting http://localhost:1313/categories/ or http://localhost:1313/categories/bar/

Code Value
{{ .Data.Singular }} category
{{ .Data.Plural }} categories

If you really need to compare strings, see:

There’s also an undocumented function strings.Contains

{{ $haystack := "The big brown dog" }}
{{ $needle := "brown" }}
{{ strings.Contains $haystack $needle }} --> true

There’s no regular expression match in your code, as far as i can see. “categories/*” is a glob. Where it a regular expression, then it would match the word “categories” followed by any number of slashes (also none). The same goes for “tags/*”.

Hugo provides findRE for regular expression matching:

So something like

{{ $result := findRE .Permalink "categories/.*"}}
{{ if gt (len $result) 0 }}
{{ ... code for categories goes here }}

might work. But that seems like overkill to me. You probably only want to know if your .Permalink contains “categories” which substr could tell you as well.

1 Like

Hello, thank your for the responses @chrillek and @jmooring .

I’v tried your solution but didn’t work for me, maybe I asked the question the wrong way.

{{ $result := findRE .Permalink "categories/.*"}} didn’t work for me (it returns an empty array when the url for example: http://localhost:1313/categories/image-processing/ ).

I will try to explain what I want in detail:
Here is a snippet of my original code

{{ if or (eq .Section "tags") (eq .Section "categories") }}
     {{ if or (eq .Permalink (`tags/` | absURL)) (eq .Permalink (`categories/` | absURL)) }}
        <h3 class="mb-3 text-dark font-weight-bold">{{.Title | markdownify}}</h3>
     {{else}}
        {{ $result := findRE .Permalink "categories/.*"}}
        <p>{{$result}}</p>
        <h4 class="mb-3 text-dark font-weight-bold">Showing posts from <mark>{{.Title | markdownify}}</mark></h4>
     {{ end }}
{{ else }}
    <h3 class="mb-3 text-dark font-weight-bold">{{.Title | markdownify}}</h3>
{{ end }}

The line <p>{{$result}}</p> shows [] when I’m on http://localhost:1313/categories/image-processing/.

What I want is to customize the h4 tag, so for http://localhost:1313/categories/some_path_here/ (but not in http://localhost:1313/categories/) to show <h4 class="mb-3 text-dark font-weight-bold">Showing posts from <mark>{{.Title | markdownify}}</mark> category</h4> for categories route.

And the same for tags route, so for http://localhost:1313/tags/some_other_path_here/ (but not in http://localhost:1313/tags/) to show <h4 class="mb-3 text-dark font-weight-bold">Showing posts from <mark>{{.Title | markdownify}}</mark> tag</h4>

Thanks your so much man

My bad: The syntax is findRE pattern input, so it should be
{{ $result := findRE "categories/.*" .Permalink}}

In any case, you cannot check for equality like you do (eq .Permalink "tags/"), because these strings will not be equal. You might, as I’d said before, use substr.

1 Like

Thank you, @chrillek it’s working

{{ if or (eq .Section "tags") (eq .Section "categories") }}
   {{ if or (eq .Permalink (`tags/` | absURL)) (eq .Permalink (`categories/` | absURL)) }}
      <h3 class="mb-3 text-dark font-weight-bold">{{.Title | markdownify}}</h3>
   {{else}}
      {{ $result_categories := findRE "categories/.*" .Permalink}}
      {{ if gt (len $result_categories) 0 }}
         <h4 class="mb-3 text-dark font-weight-bold">Showing posts from <mark>{{.Title | markdownify}}</mark> category</h4>
      {{else}}
         <h4 class="mb-3 text-dark font-weight-bold">Showing posts from <mark>{{.Title | markdownify}}</mark> tag</h4>
      {{end}}
   {{ end }}
{{ else }}
    <h3 class="mb-3 text-dark font-weight-bold">{{.Title | markdownify}}</h3>
{{ end }}

Maybe it’s not the better way to do so, if it happens and you do have another better one, feel free to give it to me, I will appreciate it. have a good day.

So basically you want to differentiate between toplevel “categories” and “tags” and the sublevels.

Your first if eq .Section ... already makes sure that you’re inside either of those. Then you check for “toplevel” with eq .Permalink "tags/" ... and in the else part you know that you’re in the sublevel. So what is this RE for? You do not need it, because all you need is the name of the section – just as you used in in the first if. So your code boils down to

{{$sectionSingular := strings.TrimRight "s" .Section}} # remove trailing s from section name
 <h4 class="mb-3 text-dark font-weight-bold">Showing posts from <mark>{{.Title | markdownify}}</mark> {{sectionSingular}}</h4>

Personally, I’d refrain from adding all these class names verbatim at each and every step. If the’yre all the same anyway, define a constant like {{$classes := "mb-3 text-dark font-weight-bold"}} and use that like classes="{{$classes}}". Much cleaner, and you have to change only one location if you ever want to use mb-4 or whatever.

The same holds for <mark>{{.Title | markdownify}}</mark> - put it in a variable at the start and make your code easier to read.

1 Like

Oh yeah I didn’t think of it :smile_cat:. Big thanks man !