hugo/content/en/content-management/archetypes.md
Bjørn Erik Pedersen 41bc6f702a Squashed 'docs/' changes from 2201ac0e5..2c0125b52
2c0125b52 Remove .Site.Author
2cf8841b3 Update partialCached.md (#1924)
385487191 Update data-templates.md (#1926)
ce207e141 Remove redundant markdown and fix a few typos (#1936)
3687c2953 Make heading id linkable, take 2
45c79bea7 Make heading id linkable
b22079344 Delete duplicates the lines 557-569 and 570-582. (#1934)
0a90dc122 Rework the taxonomy variables page (#1935)
7f8979c50 Update theme
26e682a3a Update multilingual.md
d40e7693f Update postcss.md
375d75c01 Update postcss npm instructions (#1931)
63020094a Emphasize Window shell selection (#1930)
56824be2c Update configuration.md
b7b8f16b3 Docu 'Theme components': minor fix (#1929)
09dc81a05 Remove Docker from BSD page (#1927)
205fea204 netlify: Hugo 0.108.0
6abe49c28 Merge commit 'da670c38ee63a7fef25e2b9f42519232055b60dc'
12b59a4c5 docs: Add basic doc for wrapStandAloneImageWithinParagraph etc.
ba07bd970 dartsass: Add sourceMapIncludeSources option

git-subtree-dir: docs
git-subtree-split: 2c0125b5290494d49334606c451446ebd9df3c21
2022-12-20 11:04:41 +01:00

3 KiB

title linkTitle description keywords categories menu toc weight aliases
Archetypes Archetypes Archetypes are templates used when creating new content.
archetypes
generators
metadata
front matter
content management
docs quicklinks
parent weight
content-management 140
true 140
/content/archetypes/

What are Archetypes?

Archetypes are content template files in the archetypes directory of your project that contain preconfigured front matter and possibly also a content disposition for your website's content types. These will be used when you run hugo new.

The hugo new uses the content-section to find the most suitable archetype template in your project. If your project does not contain any archetype files, it will also look in the theme.

{{< code file="archetype-example.sh" >}} hugo new posts/my-first-post.md {{< /code >}}

The above will create a new content file in content/posts/my-first-post.md using the first archetype file found of these:

  1. archetypes/posts.md
  2. archetypes/default.md
  3. themes/my-theme/archetypes/posts.md
  4. themes/my-theme/archetypes/default.md

The last two list items are only applicable if you use a theme and it uses the my-theme theme name as an example.

Create a New Archetype Template

A fictional example for the section newsletter and the archetype file archetypes/newsletter.md. Create a new file in archetypes/newsletter.md and open it in a text editor.

{{< code file="archetypes/newsletter.md" >}}

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

Insert Lead paragraph here.

New Cool Posts

{{ range first 10 ( where .Site.RegularPages "Type" "cool" ) }}

  • {{ .Title }} {{ end }} {{< /code >}}

When you create a new newsletter with:

hugo new newsletter/the-latest-cool.stuff.md

It will create a new newsletter type of content file based on the archetype template.

Note: the site will only be built if the .Site is in use in the archetype file, and this can be time consuming for big sites.

The above newsletter type archetype illustrates the possibilities: The full Hugo .Site and all of Hugo's template funcs can be used in the archetype file.

Directory based archetypes

Since Hugo 0.49 you can use complete directories as archetype templates. Given this archetype directory:

archetypes
├── default.md
└── post-bundle
    ├── bio.md
    ├── images
    │   └── featured.jpg
    └── index.md
hugo new --kind post-bundle posts/my-post

Will create a new folder in /content/posts/my-post with the same set of files as in the post-bundle archetypes folder. All content files (index.md etc.) can contain template logic, and will receive the correct .Site for the content's language.