Skip to content

Commit 863a812

Browse files
committed
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
1 parent f08ea02 commit 863a812

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

create/content_template_handler.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package create
1616
import (
1717
"bytes"
1818
"fmt"
19+
"path/filepath"
1920
"strings"
2021
"time"
2122

@@ -43,6 +44,12 @@ type ArchetypeFileData struct {
4344
// on the presence of language code in the filename.
4445
Site *hugolib.Site
4546

47+
// Name will in most cases be the same as TranslationBaseName, e.g. "my-post".
48+
// But if that value is "index" (bundles), the Name is instead the owning folder.
49+
// This is the value you in most cases would want to use to construct the title in your
50+
// archetype template.
51+
Name string
52+
4653
// The target content file. Note that the .Content will be empty, as that
4754
// has not been created yet.
4855
source.File
@@ -51,7 +58,7 @@ type ArchetypeFileData struct {
5158
const (
5259
// ArchetypeTemplateTemplate is used as initial template when adding an archetype template.
5360
ArchetypeTemplateTemplate = `---
54-
title: "{{ replace .TranslationBaseName "-" " " | title }}"
61+
title: "{{ replace .Name "-" " " | title }}"
5562
date: {{ .Date }}
5663
draft: true
5764
---
@@ -84,9 +91,17 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile
8491
sp := source.NewSourceSpec(s.Deps.Cfg, s.Deps.Fs)
8592
f := sp.NewFileInfo("", targetPath, false, nil)
8693

94+
name := f.TranslationBaseName()
95+
if name == "index" || name == "_index" {
96+
// Page bundles; the directory name will hopefully have a better name.
97+
dir := strings.TrimSuffix(f.Dir(), helpers.FilePathSeparator)
98+
_, name = filepath.Split(dir)
99+
}
100+
87101
data := ArchetypeFileData{
88102
Type: kind,
89103
Date: time.Now().Format(time.RFC3339),
104+
Name: name,
90105
File: f,
91106
Site: s,
92107
}

0 commit comments

Comments
 (0)