Skip to content

Commit

Permalink
create: Provide .Name to the archetype templates
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bep committed Jan 28, 2018
1 parent f08ea02 commit 863a812
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion create/content_template_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package create
import (
"bytes"
"fmt"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -43,6 +44,12 @@ type ArchetypeFileData struct {
// on the presence of language code in the filename.
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
// has not been created yet.
source.File
Expand All @@ -51,7 +58,7 @@ type ArchetypeFileData struct {
const (
// ArchetypeTemplateTemplate is used as initial template when adding an archetype template.
ArchetypeTemplateTemplate = `---
title: "{{ replace .TranslationBaseName "-" " " | title }}"
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
---
Expand Down Expand Up @@ -84,9 +91,17 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile
sp := source.NewSourceSpec(s.Deps.Cfg, s.Deps.Fs)
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{
Type: kind,
Date: time.Now().Format(time.RFC3339),
Name: name,
File: f,
Site: s,
}
Expand Down

0 comments on commit 863a812

Please sign in to comment.