Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .Site to archetypes templating #1629

Closed
tucnak opened this issue Nov 27, 2015 · 5 comments · Fixed by #3607
Closed

Add .Site to archetypes templating #1629

tucnak opened this issue Nov 27, 2015 · 5 comments · Fixed by #3607
Assignees
Milestone

Comments

@tucnak
Copy link

tucnak commented Nov 27, 2015

Aren't we able to template archetypes atm? For instance, in this way:

+++
title: ""
description: ""
url: "/post/{{ .Slug }}"
+++
@bep
Copy link
Member

bep commented Nov 27, 2015

No. And please use https://discuss.gohugo.io/ for questions.

@bep bep closed this as completed Nov 27, 2015
@tucnak
Copy link
Author

tucnak commented Nov 27, 2015

This is not a question, but a feature request, what's wrong with it?

@bep bep reopened this Nov 27, 2015
@bep
Copy link
Member

bep commented Nov 27, 2015

To me it looked very much like a question.

@tucnak
Copy link
Author

tucnak commented Nov 27, 2015

I was just wondering if we could implement any templating on archetypes, it makes so much sense. Btw, #452 seems to be related as well.

@bep bep added this to the v0.24 milestone Jun 17, 2017
@bep bep removed the Wish label Jun 17, 2017
@bep bep self-assigned this Jun 17, 2017
bep added a commit to bep/hugo that referenced this issue Jun 18, 2017
This commit removes the fragile front matter decoding, and takes the provided archetype file as-is and processes it as a template.

This also means that we no longer will attempt to fill in default values for `title` and `date`.

The upside is that it is now easy to create these values in a dynamic way:

```toml
+++
title = {{ .Name | title }}
date = {{ .Date }}
draft = true
+++
```

You can currently use all of Hugo's template funcs, but the data context is currently very shallow:

* `.Type` gives the archetype kind provided
* `.Name` gives the target file name without extension.
* `.Path` gives the target file name
* `.Date` gives the current time as RFC3339 formatted string

The above  will probably be extended in gohugoio#1629.

Fixes gohugoio#452
Updates gohugoio#1629
bep added a commit that referenced this issue Jun 18, 2017
This commit removes the fragile front matter decoding, and takes the provided archetype file as-is and processes it as a template.

This also means that we no longer will attempt to fill in default values for `title` and `date`.

The upside is that it is now easy to create these values in a dynamic way:

```toml
+++
title = {{ .BaseFileName | title }}
date = {{ .Date }}
draft = true
+++
```

You can currently use all of Hugo's template funcs, but the data context is currently very shallow:

* `.Type` gives the archetype kind provided
* `.Name` gives the target file name without extension.
* `.Path` gives the target file name
* `.Date` gives the current time as RFC3339 formatted string

The above  will probably be extended in #1629.

Fixes #452
Updates #1629
@bep bep changed the title Archetypes templating Add .Site to archetypes templating Jun 18, 2017
bep added a commit to bep/hugo that referenced this issue Jun 19, 2017
This commit completes the "The Revival of the Archetypes!"

If `.Site` is used in the arcetype template, the site is built and added to the template context.

Note that this may be potentially time consuming for big sites.

A more complete example would then be for the section `newsletter` and the archetype file `archetypes/newsletter.md`:

```
---
title: "{{ replace .TranslationBaseName "-" " " | title }}"
date: {{ .Date }}
tags:
- x
categories:
- x
draft: true
---

<!--more-->

{{ range first 10 ( where .Site.RegularPages "Type" "cool" ) }}
* {{ .Title }}
{{ end }}
```

And then create a new post with:

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

**Hot Tip:** If you set the `newContentEditor` configuration variable to an editor on your `PATH`, the newly created article will be opened.

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.

Fixes gohugoio#1629
@bep bep closed this as completed in #3607 Jun 19, 2017
bep added a commit that referenced this issue Jun 19, 2017
This commit completes the "The Revival of the Archetypes!"

If `.Site` is used in the arcetype template, the site is built and added to the template context.

Note that this may be potentially time consuming for big sites.

A more complete example would then be for the section `newsletter` and the archetype file `archetypes/newsletter.md`:

```
---
title: "{{ replace .TranslationBaseName "-" " " | title }}"
date: {{ .Date }}
tags:
- x
categories:
- x
draft: true
---

<!--more-->

{{ range first 10 ( where .Site.RegularPages "Type" "cool" ) }}
* {{ .Title }}
{{ end }}
```

And then create a new post with:

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

**Hot Tip:** If you set the `newContentEditor` configuration variable to an editor on your `PATH`, the newly created article will be opened.

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.

Fixes #1629
bep added a commit that referenced this issue Feb 14, 2022
93f7baf80 netlify: Hugo 0.92.2
ecd8a6b6b Merge branch 'tempv0.92.2'
ee04caf9f Fix validation of Page Kind in cascade target map
3fce03ed2 Remove the "check" command
70ad71b25 Fix typo (#1651)
dfd1dc570 Fix internal link (#1650)
b5e0e0d38 Update rotten Fedora package link
78276390a Add missing word in types.md (#1645)
45eb945f8 Remove hugo gen autocomplete
2f84d0896 netlify: Hugo 0.92.1
b55f5773b Update complement.md
7013ee43e Remove deprecated RSSLink from page variables
1c3fb6ed3 Remove deprecated page vars (#1638)
9bd0af3ab Update union.md
4576487a0 Update symdiff.md
58f1b26c7 Update starter-kits.md (#1521)
285356c1d Move inline partials section (#1635)
4d08625d2 Improve directions for editing Win 10 path env var (#1632)
41d9c7804 Update page content (#1629)
d05536cad Removed unpaired quote character (#1630)
3b0fdcd88 netlify: Bump to Hugo 0.92.0
cdb068346 docs: Regenerate docshelper
778503ca8 Merge commit 'a8e9fc699a6ff7d578f97a7c553ce844efad8fdb'
2d2401c91 docs: Add dependency table to maintainance page
6bc95c386 Add hugo.Deps
19d0b5e23 docs. Regen CLI docs
76a750d96 docs: Regenerate CLI docs
70156962a docs: Regenerate docshelper

git-subtree-dir: docs
git-subtree-split: 93f7baf80bd03176ad44701cd2452433d492ea0b
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants