Applying condition to a single page in section?

Hi there,

a Hugo partial condition in head works fine for list page such as {{ if eq .Section "contact" }} but there is no list page in the section contact. I have tried multiple variations but failed.

I have tried these methods out of desperation:

  • {{ if and .IsPage (eq .Section "contact") }}
  • {{ if and (eq .Section "contact") (eq .IsPage true) }}

How can I tell Hugo that if section “contact” single page not list page then add this script??

Thanks

I have read your post several times, and I still don’t understand the question.

Hello @jmooring

On my website and more specifically only on the contact page I want to include the leaflet css/js in the head tag. Since I only have map on the contact page it is not useful to have css and js on all pages.

I cannot make Hugo work to say if the page is /contact/ then in <head> add <script>leaflet JS</script>

Any idea on how to let Hugo only add a script if the page is a contact page?

the directory structure is like this /content/en/contact/index.md

Thanks

With that content structure, your “contact” page is not a section. Its .Kind is page. Test for the .Path instead.

Hi @jmooring

I try

  • {{ if PAGE.Path "contact" }}
  • {{ if .PAGE.Path "contact" }}
  • {{ if PAGE.Path "/contact" }}
  • {{ if .PAGE.Path "/contact" }}

But no success :frowning:

I also try

  • {{ if eq .Path "contact" }}
  • {{ if eq .Path "/contact" }}
  • {{ if eq .Path "contact/" }}
  • {{ if eq .Path "/contact/" }}

Again no success I remember the method I am trying to use that I learnt 2-3 years ago were working previously.So what is the solution or trick now with the newer version of Hugo I am unable to understand.

Thanks

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

Works great, assuming the current context is the page.

1 Like

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