From 662e12f348a638a6fcc92a416ee7f7c2a7ef8792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 18 Jun 2017 19:39:42 +0200 Subject: 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 --- {{ 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 --- create/content_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'create/content_test.go') diff --git a/create/content_test.go b/create/content_test.go index aa7ed3fcf..8b6c2c12c 100644 --- a/create/content_test.go +++ b/create/content_test.go @@ -52,13 +52,17 @@ func TestNewContent(t *testing.T) { for _, c := range cases { cfg, fs := newTestCfg() + ps, err := helpers.NewPathSpec(fs, cfg) + require.NoError(t, err) h, err := hugolib.NewHugoSites(deps.DepsCfg{Cfg: cfg, Fs: fs}) require.NoError(t, err) require.NoError(t, initFs(fs)) - s := h.Sites[0] + siteFactory := func(filename string, siteUsed bool) (*hugolib.Site, error) { + return h.Sites[0], nil + } - require.NoError(t, create.NewContent(s, c.kind, c.path)) + require.NoError(t, create.NewContent(ps, siteFactory, c.kind, c.path)) fname := filepath.Join("content", filepath.FromSlash(c.path)) content := readFileFromFs(t, fs.Source, fname) -- cgit v1.2.3