Skip to content

Create pages from _content.gotmpl #12427

@bep

Description

@bep

Note

There will be support for other data sources, e.g. _content.json, but that's outside the scope of this proposal.

I have experimented and thought long and hard about where to start re. generating Pages from data sources. The only scripting language we have built into Hugo is currently Go Templates. It works great as a templating language for HTML markup, but it's a little clunky for more advanced logic. But it's what we have, it's very familiar to many, and we have already a large API set up ready to use.

So, I suggest we start there by introducing a new reserved name identifier in the content file system _content.* where the first extension will be gotmpl, i.e. _content.gotmpl:

content
├── _index.md
└── posts
    ├── _content.gotmpl
    └── _index.md

A short example of how it may look:

{{ $logo := resources.Get "logo.jpg" }}
{{ $mydataFile := resources.Get "mydata.json" }}
{{ $eTag := $mydataFile | md5 }}

{{/* Allow Hugo to use a cached version if possible. */}}
{{ if not ($.UseCached $eTag) }}
   {{ $m :=  $mydataFile | transform.Unmarshal }}
   {{ range $k, $v := $m }}
       {{ $ppath := $k }}
       {{ $content := dict "type" "text" "value" "**Hello World**"  "markup" "markdown" }}
       {{ $.AddPage  (dict "kind" "page" "path" $ppath "title" $k "content" $content) }}
       {{ $.AddResource  (dict "path" (printf "%s/%s" $ppath $logo.Name) "resource" $logo "name" $logo.Name) }}
   {{ end }}
{{ end }

For bundled Resources I think we can say that you would either:

  • Reference an existing Resource. It would be accessible in a page's .Resources, but the .Permalink would point to the original's.
  • Or pass it in as content.

As this is early in the build process, the context passed to the template will be limited:

  • You can access .Site.Title and similar and i18n would work as expected.
  • But .Site.RegularPages will return empty.
  • All paths will be relative to the _content.gotmpl file in the content tree.

/cc @jmooring

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions