Rendering content based on the url

Hello, I’m trying to show some styles conditionally based on the url. Maybe I am going about this the wrong way but my initial thought was to check the url and based on that, show the styles.

This works but only on the specific page.

{{ if eq .RelPermalink "/my-section/" }}
    {{ $styles := resources.Get "scss/styles.scss" | toCSS | minify | fingerprint }}
    <link rel="stylesheet" href="{{ $styles.Permalink | relURL }}">
{{ end }}

Once I navigate within that section of my site the sites no longer render. For example on this page the above logic breaks.

So how could I check for if .RelPermalink contains "/my-section"?

Again, if there is a simpler/better/cleaner way to go about this I’m all ears.


Try if in instead if eq

You can also do it like this if all the URLs in that section begin with /my-section/

{{ if hasPrefix .RelPermalink "/my-section/"}}
   <!--- Your code here -->
   {{ end }}
1 Like

@tut Thanks a lot. That seems to be working.

This also seemed to get the job done. Thanks for the help.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.