Forem

Zak Miller
Zak Miller

Posted on • Originally published at zakmiller.com

1 2

Hugo - How to Filter Posts With a Tag

#go

While recently working on a website, I wanted to display a subset of my posts based on a particular tag. I hadn't had any prior experience with the go templating engine, so this took me a bit longer to figure out than I'd care to admit, so I thought I'd share it with all of you.

This example applies anytime you want to filter any list of pages with a taxonomy.

    <ul>
      {{ range where .Paginator.Pages ".Params.tags" "intersect" (slice "popular")}}
      <li>
        <span>{{ .Date.Format (.Site.Params.dateFormat | default "January 2, 2006" ) }}</span>
        <a href="{{ .URL }}">{{ .Title }}</a>
      </li>
      {{ end }}
    </ul>

The first line is really all that matters. We're range-ing over the pages, grabbing the tags from each of those pages and seeing if they intersect with a new slice that only contains popular.

A contains operator is what I would expect to use here, but as far as I can tell there isn't one, so you have to use this intersect workaround.

I hope that was helpful! If you have any questions please let me know.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay