How to exclude a partial from head.html for some pages?

I want to exclude the partials adsense.html and analytics.html from some pages (for testing load performance i.e.). Rather than setting a Param to include it I set one to exclude it. It works, but I feel it’s not the correct way.

For *.md pages that I want to exclude the code:

---
google: false
---

In head.html

{{ if eq .Params.google false }}
{{ else }}
{{partial "analytics.html" .}}
{{partial "adsense.html" .}}
{{ end }}

Is there a better way?

{{ if .Params.google }}
  {{ partial "analytics.html" . }}
  {{ partial "adsense.html" . }}
{{ end }}

This code will not show up the partials on any page.

On “normal” pages I have no google: true. So the logic I need is: if no google: *** show partials.

There is a better way:

{{ if ne .Params.google false }}
{{partial "analytics.html" .}}
{{partial "adsense.html" .}}
{{ end }}

:slight_smile:

1 Like

A bit off topic. I would strongly recommend you to resign from AdSense in lieu of other solutions. You using Hugo to get performance boost and good Core Web Vitals where AdSense will kill that.

@nfriedli
Looks much cleaner and works perfectly. Thank you!

@idarek
Yeah, a bit off topic. AdSense was only an example, you can use any code that you want to remove from specific pages with that method.

But you are of course right that AdSense is quite a drag. But there are methods to lazyload Adsense ads as long as they are not AutoAds. Certainly worthwhile looking into that. I do that later. I did look into Youtube though and implemented THIS METHOD on my Hugo site for much faster embedded video loading.

Have to add, once one gets the basic idea how Hugo works it’s quite easy to implement whatever js/jquery wizardry you see somewhere.

An off-topic update. I tried THIS METHOD with the Hugo Youtube shortcode and there wasn’t really a difference. Put this code speeds youtube links up a lot: GitHub - paulirish/lite-youtube-embed: A faster youtube embed.
In PageSpeed numbers for a page with only some HTML and one Youtube embed::

62 Hugo Shortcode
64 Hugo Shortcode + Lazy Wrapper
100 Paul Irish Github code

But note that the Paul Irish code does not display the title.

2 Likes

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