diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-06-18 19:39:42 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-06-19 10:47:00 +0200 |
commit | 662e12f348a638a6fcc92a416ee7f7c2a7ef8792 (patch) | |
tree | f7ffad68d882a9e25e08c5405c41062e078e76d7 /create/content_template_handler.go | |
parent | 422057f60709696bbbd1c38c9ead2bf114d47e31 (diff) |
commands, create: Add .Site to the archetype templates
This commit completes the "The Revival of the Archetypes!"
If `.Site` is used in the arcetype template, the site is built and added to the template context.
Note that this may be potentially time consuming for big sites.
A more complete example would then be for the section `newsletter` and the archetype file `archetypes/newsletter.md`:
```
---
title: "{{ replace .TranslationBaseName "-" " " | title }}"
date: {{ .Date }}
tags:
- x
categories:
- x
draft: true
---
<!--more-->
{{ range first 10 ( where .Site.RegularPages "Type" "cool" ) }}
* {{ .Title }}
{{ end }}
```
And then create a new post with:
```bash
hugo new newsletter/the-latest-cool.stuff.md
```
**Hot Tip:** If you set the `newContentEditor` configuration variable to an editor on your `PATH`, the newly created article will be opened.
The above _newsletter type archetype_ illustrates the possibilities: The full Hugo `.Site` and all of Hugo's template funcs can be used in the archetype file.
Fixes #1629
Diffstat (limited to 'create/content_template_handler.go')
-rw-r--r-- | create/content_template_handler.go | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/create/content_template_handler.go b/create/content_template_handler.go index 9903c3dec..48e7f3b4b 100644 --- a/create/content_template_handler.go +++ b/create/content_template_handler.go @@ -18,6 +18,7 @@ import ( "fmt" "time" + "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/source" "github.com/gohugoio/hugo/hugolib" @@ -25,9 +26,30 @@ import ( "github.com/spf13/afero" ) +// ArchetypeFileData represents the data available to an archetype template. +type ArchetypeFileData struct { + // The archetype content type, either given as --kind option or extracted + // from the target path's section, i.e. "blog/mypost.md" will resolve to + // "blog". + Type string + + // The current date and time as a RFC3339 formatted string, suitable for use in front matter. + Date string + + // The Site, fully equipped with all the pages etc. Note: This will only be set if it is actually + // used in the archetype template. Also, if this is a multilingual setup, + // this site is the site that best matches the target content file, based + // on the presence of language code in the filename. + Site *hugolib.Site + + // The target content file. Note that the .Content will be empty, as that + // has not been created yet. + *source.File +} + const ( archetypeTemplateTemplate = `+++ -title = "{{ replace .BaseFileName "-" " " | title }}" +title = "{{ replace .TranslationBaseName "-" " " | title }}" date = {{ .Date }} draft = true +++` @@ -44,14 +66,11 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile sp := source.NewSourceSpec(s.Deps.Cfg, s.Deps.Fs) f := sp.NewFile(targetPath) - data := struct { - Type string - Date string - *source.File - }{ + data := ArchetypeFileData{ Type: kind, Date: time.Now().Format(time.RFC3339), File: f, + Site: s, } if archetypeFilename == "" { @@ -67,11 +86,12 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile // Reuse the Hugo template setup to get the template funcs properly set up. templateHandler := s.Deps.Tmpl.(tpl.TemplateHandler) - if err := templateHandler.AddTemplate("_text/archetype", string(archetypeTemplate)); err != nil { + templateName := "_text/" + helpers.Filename(archetypeFilename) + if err := templateHandler.AddTemplate(templateName, string(archetypeTemplate)); err != nil { return nil, fmt.Errorf("Failed to parse archetype file %q: %s", archetypeFilename, err) } - templ := templateHandler.Lookup("_text/archetype") + templ := templateHandler.Lookup(templateName) var buff bytes.Buffer if err := templ.Execute(&buff, data); err != nil { |