What is the best way to set up a one-off template?

I’m trying to find a way to generate one special page in a custom text format (located at /website.data) using a template. Given that the template will only ever be used in one place to generate a single page (website.data), what would be the best way to set this up in hugo?

My intuition says I should be defining a custom output format for .data, and then placing a template file in ./layouts somewhere. My intimidate thought was to place the template file in the root of layouts like I did for the website’s landing page template (/index.html). That didn’t work as expected.

Any recommendations on how I should proceed?

Apologies if this has been asked before and thank you for your time and patience.

Layout in layouts/SECTION/mytemplate.html (use single.html as template) and then add in the frontmatter of the page you want to use this: layout: mytemplate.

Use _default for SECTION for a default layout, or use blog if your page is in the content/blog/ directory and you want to style pages in each section different.

see Front Matter | Hugo


the layout Hugo should select from the lookup order when rendering the content. If a type is not specified in the front matter, Hugo will look for the layout of the same name in the layout directory that corresponds with a content’s section. See “Defining a Content Type”

1 Like

Thank you for your detailed response. If I understood you correctly, the steps I need to follow for custom output templates to work are:

  1. place my template data.data in /layouts/_default/data.data
  2. place my content file _index.data in /content/_private_/_index.data
  3. the contents of _index.data should only consist of front-matter and nothing else:
    "title": "My custom data"
  1. the contents of the template file data.data should be the code that generates the file.

I’ve followed these steps, but hugo doesn’t seem to be detecting or parsing the front matter. Instead it copies the content file to the public directory verbatim (i.e /content/_private_/_index.data/public/_private_/_index.data).

If it is any help, the following is my custom output types configuration:

        suffixes = ["data"]

        mediaType = "application/x-my-custom-data"
        rel = "canonical"
        isPlainText = true
        isHTMLText = false
        noUgly = false
        permalinkable = true

I can’t wrap my head around your custom data format. If _index.data in /content/_private_/_index.data is the content and /layouts/_default/data.data transforms that content then you are right.

It’s probably best to have some form of minimal test sample in a public repo somewhere.

I never used JSON as frontmatter, but the docs talk about a new line:

A single JSON object surrounded by ‘ { ’ and ‘ } ’, followed by a new line.

Maybe your editor removed that new line?

Also, pretty sure you get some form of output from the CLI command. Maybe run hugo and have a look what’s inside of public. Afterwards. I remember having a hard time finding the right template for a custom content type that defined it’s own media type. Maybe your template does not get read.

What is the media type of the output? Something known like JSON or HTML? Maybe try without your own media type first.

PS: What happens if you rename it to layouts/_default/single.data or add "layout": "data" to your JSON object in the content?

1 Like

What happens if you rename it to layouts/_default/single.data or add "layout": "data" to your JSON object in the content?

It works! :tada:

… but only when I changed the the content file extension to md. If I rename the output template to list.data, rename the content file to _index.md, and explicitly set the output format to data, hugo renders the custom output.

I have no problem with renaming the content file so this finally solved the issue. Thank you Patrick for helping me. I really appreciate it!