Debian Codegraph @

docs/content/en/content-management/archetypes.md

raw
1 ---
2 title: Archetypes
3 description: An archetype is a template for new content.
4 categories: []
5 keywords: []
6 aliases: [/content/archetypes/]
7 ---
8
9 ## Overview
10
11 A content file consists of [front matter](g) and markup. The markup is typically Markdown, but Hugo also supports other [content formats](g). Front matter can be TOML, YAML, or JSON.
12
13 The `hugo new content` command creates a new file in the `content` directory, using an archetype as a template. This is the default archetype:
14
15 {{< code-toggle file=archetypes/default.md fm=true >}}
16 title = '{{ replace .File.ContentBaseName `-` ` ` | title }}'
17 date = '{{ .Date }}'
18 draft = true
19 {{< /code-toggle >}}
20
21 When you create new content, Hugo evaluates the [template actions](g) within the archetype. For example:
22
23 ```sh
24 hugo new content posts/my-first-post.md
25 ```
26
27 With the default archetype shown above, Hugo creates this content file:
28
29 {{< code-toggle file=content/posts/my-first-post.md fm=true >}}
30 title = 'My First Post'
31 date = '2023-08-24T11:49:46-07:00'
32 draft = true
33 {{< /code-toggle >}}
34
35 You can create an archetype for one or more [content types](g). For example, use one archetype for posts, and use the default archetype for everything else:
36
37 ```tree
38 archetypes/
39 ├── default.md
40 └── posts.md
41 ```
42
43 ## Lookup order
44
45 Hugo looks for archetypes in the `archetypes` directory in the root of your project, falling back to the `archetypes` directory in themes or installed modules. An archetype for a specific content type takes precedence over the default archetype.
46
47 For example, if you have enabled a theme named `my-theme` and you run this command:
48
49 ```sh
50 hugo new content posts/my-first-post.md
51 ```
52
53 The archetype lookup order is:
54
55 1. `archetypes/posts.md`
56 1. `themes/my-theme/archetypes/posts.md`
57 1. `archetypes/default.md`
58 1. `themes/my-theme/archetypes/default.md`
59
60 If none of these exists, Hugo uses a built-in default archetype.
61
62 ## Functions and context
63
64 You can use any template [function](g) within an archetype. As shown above, the default archetype uses the [`strings.Replace`][] function to replace hyphens with spaces when populating the title in front matter.
65
66 Archetypes receive the following [context](g):
67
68 `Date`
69 : (`string`) The current date and time, formatted in compliance with RFC3339.
70
71 `File`
72 : (`hugolib.fileInfo`) Returns [file information][] for the current page.
73
74 `Type`
75 : (`string`) The [content type](g) inferred from the top-level directory name, or as specified by the `--kind` flag passed to the `hugo new content` command.
76
77 `Site`
78 : (`page.Site`) The current `Site` object.
79
80 ## Date format
81
82 To insert date and time with a different format, use the [`time.Now`][] function:
83
84 {{< code-toggle file=archetypes/default.md fm=true >}}
85 title = '{{ replace .File.ContentBaseName `-` ` ` | title }}'
86 date = '{{ time.Now.Format "2006-01-02" }}'
87 draft = true
88 {{< /code-toggle >}}
89
90 ## Include content
91
92 Although typically used as a front matter template, you can also use an archetype to populate content.
93
94 For example, in a documentation site you might have a section (content type) for functions. Every page within this section should follow the same format: a brief description, the function signature, examples, and notes. We can pre-populate the page to remind content authors of the standard format.
95
96 ````md {file="archetypes/functions.md"}
97 ---
98 date: '{{ .Date }}'
99 draft: true
100 title: '{{ replace .File.ContentBaseName `-` ` ` | title }}'
101 ---
102
103 A brief description of what the function does, using simple present tense in the third person singular form. For example:
104
105 `someFunction` returns the string `s` repeated `n` times.
106
107 ## Signature
108
109 ```text
110 func someFunction(s string, n int) string
111 ```
112
113 ## Examples
114
115 One or more practical examples, each within a fenced code block.
116
117 ## Notes
118
119 Additional information to clarify as needed.
120 ````
121
122 Although you can include [template actions](g) within the content body, remember that Hugo evaluates these once---at the time of content creation. In most cases, place template actions in a [template](g) where Hugo evaluates the actions every time you [build](g) the site.
123
124 ## Leaf bundles
125
126 You can also create archetypes for [leaf bundles](g).
127
128 For example, in a photography site you might have a section (content type) for galleries. Each gallery is leaf bundle with content and images.
129
130 Create an archetype for galleries:
131
132 ```tree
133 archetypes/
134 ├── galleries/
135 │ ├── images/
136 │ │ └── .gitkeep
137 │ └── index.md <-- same format as default.md
138 └── default.md
139 ```
140
141 Subdirectories within an archetype must contain at least one file. Without a file, Hugo will not create the subdirectory when you create new content. The name and size of the file are irrelevant. The example above includes a&nbsp;`.gitkeep` file, an empty file commonly used to preserve otherwise empty directories in a Git repository.
142
143 To create a new gallery:
144
145 ```sh
146 hugo new galleries/bryce-canyon
147 ```
148
149 This produces:
150
151 ```tree
152 content/
153 ├── galleries/
154 │ └── bryce-canyon/
155 │ ├── images/
156 │ │ └── .gitkeep
157 │ └── index.md
158 └── _index.md
159 ```
160
161 ## Specify archetype
162
163 Use the `--kind` command line flag to specify an archetype when creating content.
164
165 For example, let's say your site has two sections: articles and tutorials. Create an archetype for each content type:
166
167 ```tree
168 archetypes/
169 ├── articles.md
170 ├── default.md
171 └── tutorials.md
172 ```
173
174 To create an article using the articles archetype:
175
176 ```sh
177 hugo new content articles/something.md
178 ```
179
180 To create an article using the tutorials archetype:
181
182 ```sh
183 hugo new content --kind tutorials articles/something.md
184 ```
185
186 [`strings.Replace`]: /functions/strings/replace/
187 [`time.Now`]: /functions/time/now/
188 [file information]: /methods/page/file/