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 · 4 comments

Comments

Projects
None yet
4 participants
@tucnak

tucnak commented Nov 27, 2015

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

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

This comment has been minimized.

Show comment
Hide comment
@bep

bep Nov 27, 2015

Member

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

Member

bep commented Nov 27, 2015

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

@bep bep closed this Nov 27, 2015

@tucnak

This comment has been minimized.

Show comment
Hide comment
@tucnak

tucnak Nov 27, 2015

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

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

This comment has been minimized.

Show comment
Hide comment
@bep

bep Nov 27, 2015

Member

To me it looked very much like a question.

Member

bep commented Nov 27, 2015

To me it looked very much like a question.

@tucnak

This comment has been minimized.

Show comment
Hide comment
@tucnak

tucnak 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.

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.

@spf13 spf13 added the area/content label Nov 28, 2015

@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

create: Use archetype template as-is as a Go template
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 #1629.

Fixes #452
Updates #1629

bep added a commit that referenced this issue Jun 18, 2017

create: Use archetype template as-is as a Go template
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 from Archetypes templating to Add .Site to archetypes templating Jun 18, 2017

bep added a commit to bep/hugo that referenced this issue Jun 19, 2017

commands, create: Add .Site to the archetype templates
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 bep closed this in #3607 Jun 19, 2017

bep added a commit that referenced this issue Jun 19, 2017

commands, create: Add .Site to the archetype templates
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment