Custom Output Formats

Hi,

I cannot figure out how to make Custom Output Formats work - probably because I lack some conceptual knowledge…

Say I want my content to be rendered both as html and (say) csv.

I then need to configure an correspondig “output”-section in my config and I need a template for the csv-rendering.

My first question is now: What filename do I use for the template and where do I put it?

I assume it would be something like “single.csv.cvs” and it would go where the html-templates go - is this correct?

And my second question is what would would be the url that a csv-version of a page would be accessible? I cannot find anything in the documentation.

This is probably a very trivial thing to ask but at the moment I cannot get it to work and cannot find any worked out examples.

Many thanks!

first read this

I used it heavy in my sample

look in ~/config/ and ~/themes/photon/config.toml and templates in ~/themes/photon/layouts/_default

Well, I have read the hugo documentation page, but the problem for me with many of these pages is that they make perfect sense once you have a mental picture of of a specific aspect works but they are (at least for my taste) pretty poor for beginners that yet have to build this mental picture. Often they use terminology that is not defined anywhere and just assume background knowledge, but I digress…

What I currently don’t understand is this (I am simply trying to understand hugo, it’s not something I need):

Say I define a media-type with two extensions like this:

[mediaTypes]
[mediaTypes.“text/plain”]
suffixes = [“txt”, “hubba”]

And an output format like this:

[outputFormats]
[outputFormats.Plain]
mediaType = “text/plain”
isPlainText = true

And now I configure outputs like this:

[outputs]
page = [“html”, “plain”]

Then I implement single.html, single.plain.txt and single.plain.hubba templates (the naming convention seems to be .).

Then I can get the html-version of a post in content/posts/test.md via an url ending in /posts/test or via /posts/test/index.html.

I can get a plain-text version via /posts/test/index.txt but (surprisingly for me) /posts/test/index.hubba does not work - and why is that (after all I’ve supplied a template)?

Evidently my mental picture does not match what hugo is doing but the documentation is no help…

@morgon

There are a couple of things going on here:

  1. Regardless of how many suffixes and corresponding templates you have created for a particular media type, the number of files generated for a particular output format is 1.

  2. There’s a bug… I think.

    For each of the tests below, the template directory was identical:

    layouts/
    └── _default/
        ├── single.a.foo
        └── single.b.bar
    

Test 1

[mediaTypes]
  [mediaTypes."text/plain"]
    suffixes = ["foo","bar"]

[outputFormats]
  [outputFormats.a]
    mediaType = "text/plain"
    isPlainText = true
  [outputFormats.b]
    mediaType = "text/plain"
    isPlainText = true

[outputs]
  page = ["a","b"]

WARN 2021/06/22 07:57:18 found no layout file for “b” for kind “page”

Test 2

Same as Test 1, but I have reversed the order of the items in the suffixes array.

[mediaTypes]
  [mediaTypes."text/plain"]
    suffixes = ["bar","foo"]  <-- Order is reversed compared to Test 1

[outputFormats]
  [outputFormats.a]
    mediaType = "text/plain"
    isPlainText = true
  [outputFormats.b]
    mediaType = "text/plain"
    isPlainText = true

[outputs]
  page = ["a","b"]

WARN 2021/06/22 08:00:47 found no layout file for “a” for kind "page

Notice that now it cannot find the other template.

Minimal Reproducible Example

git clone --single-branch -b hugo-forum-topic-33481 https://github.com/jmooring/hugo-testing hugo-forum-topic-33481
cd hugo-forum-topic-33481
hugo

@bep, Please comment.

IMHO

the suffixes are used to find the correct template.
every outputFormat should only have a single suffix to work

rename one mediaType to “text/plain-a” and duplicate the definition for plain-a, it will work.

this is a weak desgin … or a bad logic inside

We currently use only the first suffix defined in layout lookups. We use all of them to detect Mime type of a given resource.

That explains the behaviour.

It should be documented somewhere.

Done.