Sort a post's categories/tags alphabetically? (micro.blog)

As near as I’ve been able to tell through countless google searches, this doesn’t seem to be possible, but in the event my google-fu is just bad, I’ll directly ask here.

(Disclaimer: I am not Hugo proficient, per se, but once upon a time I did a lot of self-hosted Moveable Type and WordPress blogging so I have enough general templating/theming experience to probably be able to follow along here.)

Currently over on micro.blog each of my blog posts displays the categories I’ve filed it in, using this code (which I don’t even remember where I found).

{{ range .Params.categories }}<a href="{{ "/categories/" }}{{ . | urlize }}">{{ . }}</a> {{ end }}

Is there no way, via sort or otherwise, to get these to display alphabetically? Weirdly, if I just add sort after range is does work but it also throws up template errors in my micro.blog account.

Theme error: Error building site: failed to render pages: render of "page" failed: "/bix_504e91/layouts/post/single.html:17:12": execute of template failed: template: post/single.html:17:12: executing "main" at : error calling sort: sequence must be provided

While this error doesn’t, for some reason, actually seem to stop the page in question from rendering do seem to cause trouble elsewhere, but mostly I just assume that if it’s barfing up an error at all, despite working, I shouldn’t do it.

So, how do I do it? Can I?

Thanks.

Give the sort an argument, should work.

I’ve tried that, with whatever kinds of arguments I’ve found so far in other discussions of sort. They all cough up errors.

Anyone know if there’s a way to pull a post’s tags into a separate list or array or whatever and then sort that alphabetically?

Yep. Give this a try

{{ $tags := .Params.tags }}
{{ sort $tags }}

Which does output [Architecture Design Technology] but how do I incorporate this idea into the original {{ range .Params.categories }}<a href="{{ "/categories/" }}{{ . | urlize }}">{{ . }}</a> {{ end }} code above, to result in a list of linked tag pages?

Have you tried?

{{ range (sort .Params.categories) }}

See the original post. (tl;dr: range sort technically works, but causes page render/site build error messages anyway.)

You need to explain more about this.

Also, what hugo version are you using?

I’m not sure what else to explain. Existing code lists the categories the blog post is in, but I can’t seem to find any method, or anyone else who has found any method, for having them display alphabetically.

I have no idea what Hugo version; as noted, this is on a Micro.blog blog; I don’t have access to that info. (I’m having similar discussions in the Micro.blog Slack, also to no avail.)

I’m not familiar with micro blog, or how you’ve integrated it with hugo. Maybe someone else can help you there.

If you are going to get help here, you’ll need to do two things:

  1. Follow the advice at Requesting Help, including sharing your code. We can’t debug without seeing code.
  2. You need to develop your Hugo theme and get it working locally

If you share your theme, we can probably assist.

It’s literally this theme with the above {{ range .Params.categories }}<a href="{{ "/categories/" }}{{ . | urlize }}">{{ . }}</a> {{ end }} code added to the ‘single.html’ template.

FWIW, I’ve since asked M.b their current Hugo version and they are still back on 0.53. I don’t know if there’s something in later versions which allow sorting tags alphabetically on a post.

I have this in the head of my single.html

		{{ if isset .Params "categories" -}}
		  {{ range $index, $name := sort .Params.categories -}}
			{{ with site.GetPage (printf "/%s" ($name | urlize )) -}} 
				<a class=ph1 href="{{ .RelPermalink }}" title="category: {{ .Name | markdownify}}">{{ .Name | markdownify}}</a> 
			{{- end}}
		  {{- end}}
		{{- end }}

I can try to shorten is a little with the DOT notation

1 Like

Oh. Hey. Hang on. That range $index, $name := sort .Params.categories bit might have just worked without any errors.

Okay, well, it worked in one Marfa edit and not another Marfa edit, but that fact and @ju52’s post led me to the why: if I in fact use even just my original code, but wrap it in an {{ if .Params.categories }} {{ end }} it all works, with no error messages about template builds. I don’t understand enough about any of this to understand why that fixes it, but it does. :man_shrugging:

{{ if isset .Params “categories” }}

Cab be, you have pages without categories???

Likely, some of your posts don’t have a value for .Params.categories, aka it’s nil