archetypes.md - hugo - [fork] hugo port for 9front
HTML git clone https://git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
archetypes.md (5743B)
---
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 ```text
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, with 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. `archetypes/default.md`
57 1. `themes/my-theme/archetypes/posts.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 [`replace`](/functions/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. See [details](/methods/page/file).
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. See [details](/methods/site/).
79
80 ## Date format
81
82 To insert date and time with a different format, use the [`time.Now`] function:
83
84 [`time.Now`]: /functions/time/now/
85
86 {{< code-toggle file=archetypes/default.md fm=true >}}
87 title = '{{ replace .File.ContentBaseName `-` ` ` | title }}'
88 date = '{{ time.Now.Format "2006-01-02" }}'
89 draft = true
90 {{< /code-toggle >}}
91
92 ## Include content
93
94 Although typically used as a front matter template, you can also use an archetype to populate content.
95
96 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.
97
98 ````text {file="archetypes/functions.md"}
99 ---
100 date: '{{ .Date }}'
101 draft: true
102 title: '{{ replace .File.ContentBaseName `-` ` ` | title }}'
103 ---
104
105 A brief description of what the function does, using simple present tense in the third person singular form. For example:
106
107 `someFunction` returns the string `s` repeated `n` times.
108
109 ## Signature
110
111 ```text
112 func someFunction(s string, n int) string
113 ```
114
115 ## Examples
116
117 One or more practical examples, each within a fenced code block.
118
119 ## Notes
120
121 Additional information to clarify as needed.
122 ````
123
124 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.
125
126 ## Leaf bundles
127
128 You can also create archetypes for [leaf bundles](g).
129
130 For example, in a photography site you might have a section (content type) for galleries. Each gallery is leaf bundle with content and images.
131
132 Create an archetype for galleries:
133
134 ```text
135 archetypes/
136 ├── galleries/
137 │ ├── images/
138 │ │ └── .gitkeep
139 │ └── index.md <-- same format as default.md
140 └── default.md
141 ```
142
143 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 `.gitkeep` file, an empty file commonly used to preserve otherwise empty directories in a Git repository.
144
145 To create a new gallery:
146
147 ```sh
148 hugo new galleries/bryce-canyon
149 ```
150
151 This produces:
152
153 ```text
154 content/
155 ├── galleries/
156 │ └── bryce-canyon/
157 │ ├── images/
158 │ │ └── .gitkeep
159 │ └── index.md
160 └── _index.md
161 ```
162
163 ## Specify archetype
164
165 Use the `--kind` command line flag to specify an archetype when creating content.
166
167 For example, let's say your site has two sections: articles and tutorials. Create an archetype for each content type:
168
169 ```text
170 archetypes/
171 ├── articles.md
172 ├── default.md
173 └── tutorials.md
174 ```
175
176 To create an article using the articles archetype:
177
178 ```sh
179 hugo new content articles/something.md
180 ```
181
182 To create an article using the tutorials archetype:
183
184 ```sh
185 hugo new content --kind tutorials articles/something.md
186 ```