Posts for: #Template

Hugo Template Render Hooks

This was a tough nut to crack. I’m totally new to Go Html Templates and Go itself, and I’ve never really used anything outside of Jekyll. It took me a minute, but I wound up coming up with this:

{{ $image := (resources.Get .Destination) }}
{{ with $image }}
    {{ if eq $image.MediaType.SubType "svg" }}
        <a data-fancybox="image" href="{{ $image.RelPermalink }}" data-caption="{{ with $.Title }}{{ . }}{{ end }}">
            <img src="{{ $image.RelPermalink }}" {{ with $.Text }} alt="{{ . }}" {{ end }} {{ with $.Title}} title="{{ . }}"{{ end }} />
        </a>
    {{ else }}
        {{ $resize := .Fill "200x165 webp q80" }}
        <a data-fancybox="image" href="{{ $image.RelPermalink }}" data-caption="{{ with $.Title }}{{ . }}{{ end }}">
            <img src="{{ $resize.RelPermalink }}" {{ with $.Text }} alt="{{ . }}" {{ end }} {{ with $.Title}} title="{{ . }}"{{ end }} />
        </a>
    {{ end }}
{{ end }}

Apparently I have a *.svg file on my blog somewhere, because that was a thing I had to catch right away for this to work. So I had to move all my images out of static and into assets for it to work, first of all. Then I had to figure out how to query a parent context, so I could fill in Text and Title with the images. That’s what the $ is in $.Text. It’s the parent context outside of $image.

[]

Lmfao

BEHOLD, FUCKING MEMES.

God damn, this thing is really flexible. Just added a layouts/shortcodes/gallery.html and slapped a little Go Html Template and some lightbox.js and whatever, and bing-o bang-o boom; motherfucker is an autogallery. BOOYAH, BITCH.

Cool as fuck.

Of course there’s a little more to it than that. Gotta do some page bundling as well, which is easy enough. Just run your hugo new post/2022-01-01-Title/index.md and dump your images into content/post/2022-01-01-Title/img.

[]

Pinning Posts in Hugo

Alright, in this post I’m going to show you how to pin posts in Hugo. But before you keep reading, I request you review the template you’re using and ensure it’s going to allow you to pin posts in the first place.

The thing you’re looking for is the index declaration in your index.html.

Example:

{{- define "content" -}}
  <section id="posts" class="posts">
    {{/* (index .Site.Paginate) */}}
    {{- $paginator := .Paginate (where (where .Site.RegularPages "Type" "post") ".Params.hiddenfromhomepage" "!=" true) }}
    {{- range $paginator.Pages -}}
      {{ .Render "summary" }}
    {{ end -}}
  </section>

For instance, in the above snipped, line number four is where you can find the $paginator that will tell you whether you’ll be able to pin posts. If you see mine, you’re good. Most are probably going to do this. If you see anything with “sortby” then you’re probably fucked, because your template is probably forcing your shit to sort by date, rather than just paginate as Hugo does by default.

[]