create: Provide .Name to the archetype templates

This value will have a better suited value to base the titles on in your archetype templates when creating bundle ´index.md` type of files.

The internal template is updates, but you will have to update any custom archetype template to use the new `.Name` variable:

```bash
---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
---
```

Fixes #4348
This commit is contained in:
Bjørn Erik Pedersen 2018-01-28 19:38:10 +01:00
parent f08ea02d24
commit 863a812e07
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F

View file

@ -16,6 +16,7 @@ package create
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"path/filepath"
"strings" "strings"
"time" "time"
@ -43,6 +44,12 @@ type ArchetypeFileData struct {
// on the presence of language code in the filename. // on the presence of language code in the filename.
Site *hugolib.Site Site *hugolib.Site
// Name will in most cases be the same as TranslationBaseName, e.g. "my-post".
// But if that value is "index" (bundles), the Name is instead the owning folder.
// This is the value you in most cases would want to use to construct the title in your
// archetype template.
Name string
// The target content file. Note that the .Content will be empty, as that // The target content file. Note that the .Content will be empty, as that
// has not been created yet. // has not been created yet.
source.File source.File
@ -51,7 +58,7 @@ type ArchetypeFileData struct {
const ( const (
// ArchetypeTemplateTemplate is used as initial template when adding an archetype template. // ArchetypeTemplateTemplate is used as initial template when adding an archetype template.
ArchetypeTemplateTemplate = `--- ArchetypeTemplateTemplate = `---
title: "{{ replace .TranslationBaseName "-" " " | title }}" title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }} date: {{ .Date }}
draft: true draft: true
--- ---
@ -84,9 +91,17 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile
sp := source.NewSourceSpec(s.Deps.Cfg, s.Deps.Fs) sp := source.NewSourceSpec(s.Deps.Cfg, s.Deps.Fs)
f := sp.NewFileInfo("", targetPath, false, nil) f := sp.NewFileInfo("", targetPath, false, nil)
name := f.TranslationBaseName()
if name == "index" || name == "_index" {
// Page bundles; the directory name will hopefully have a better name.
dir := strings.TrimSuffix(f.Dir(), helpers.FilePathSeparator)
_, name = filepath.Split(dir)
}
data := ArchetypeFileData{ data := ArchetypeFileData{
Type: kind, Type: kind,
Date: time.Now().Format(time.RFC3339), Date: time.Now().Format(time.RFC3339),
Name: name,
File: f, File: f,
Site: s, Site: s,
} }