Putting custom hugo script inside of a micro.blog page?

Hello all,

I want to add a page to my micro.blog site called “foo”. So the URL would be www.mysite.com/foo. Then on that page I want to be able to put some text and then add a list of all the pages which include the word “foo” in the title or body.

I’m trying to add a hugo script to the Page, but when it compiles it just renders the Hugo script verbatim on the page. Is there a way I can get the script to “run” when the page is compiled/published? Do I need a script tag or something similar for it to run when the page gets published?

Here is an example of the type of script I’m trying to add.
{{ define “main” }}

    {{ range (where (where .Site.Pages "Type" "post") "Section" "post") }} {{ $content := .Plain }} {{ if or (in .Title "foo") (in $content "foo") }}
  • {{ .Date.Format "2006-01-02" }} - {{ .Title }}
  • {{ end }} {{ end | sort "Date" "desc" }}
{{ end }}

So the text of the final page might be something like…

Here is my text describing foo…
— Hugo script inserted here —
Then a closing statement on the page.

There are several ways to do this. IMO, the easiest way is have /foo redirect to /categories/foo and use Micro.blog’s ability to automatically add the category foo to any post where foo is in title or content using the filters on the Categories page.

For example, my /now page is actually /categories/now (and I have a custom template for that which is more complex).

Thanks @jsonbecker. For my own education… does that mean no Hugo script tags will compile in a Pages file, only in the templates files? It feels like the Pages functionality is a micro.blog layer on top of Hugo so I’m trying to understand where the divide is.

The Page functionality is essentially injecting a post at a page URL. It’s not a Hugo template. Short codes work, but not templating, just like in any content file for Hugo. Anything that works in a blog post works.

If you want a true custom template, you have to customize your theme and add the template yourself.

@jsonbecker - I’ve been looking at your plugin for bookshop and I’m wondering if that’s what I need to create.

I could create a short code with a parameter for the text i’m searching for. Then the plugin could render that HTML. Similar to the book goals short code from Manton.

You could. I have quite a few short codes for various things. But I if you’re looking for doing a list template like that, I really think what you want is a category page. You can set a custom template for the category page that does the list style you want (seems like that list doesn’t have links, but sure).

If I were doing this as shortcode, I’d probably have the category/text as the parameter, and then your code as the interior to the shortcode. I’m not actually sure if a shortcode has access to .Site.Pages. That’d be the one thing. Also, once you’re writing a shortcode, there are other methods that work for custom pages if you know how to write a template.

For example, you can have a custom template for a post type and put content/url_you_want.md in your template and have that markdown file be empty except for top matter that includes a type:.

Thanks @jsonbecker. Yeah, going from a plain page to a Hugo template is kind of a big jump technically at the moment. This is part of what I plan to solve with a new feature I’m calling “smart pages”… It’s not ready yet, but when it’s ready it’ll automatically create the templates for you for exactly this use case, without any knowledge of Hugo.

1 Like