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 page template function #9339

Closed
bep opened this issue Jan 1, 2022 · 8 comments · Fixed by #10755
Closed

Add page template function #9339

bep opened this issue Jan 1, 2022 · 8 comments · Fixed by #10755
Assignees
Milestone

Comments

@bep
Copy link
Member

bep commented Jan 1, 2022

We have several issues discussing something like this (re partials and .Render), but I haven't found a way (or at least not a reasonably simple way) to implement it ...

... until now.

This function will return the top level data context (the Page) when called from anywhere (shortcodes, partials, render hooks, view templates).

So you would do:

partials/mypartial:

{{ render.Context.Title }}

Will print the entry Page's title.

My explanation above may not be perfect, but this is plenty useful -- the hard part is getting the name right.

/cc @regisphilibert @jmooring etc.

@bep bep added the Proposal label Jan 1, 2022
@bep
Copy link
Member Author

bep commented Jan 1, 2022

Since this "context" is always a Page, maybe we could just call it ... page, which would be analogue to the server global.

So, in the top level single.html etc. template, these will be the same:

{{ .Title }}
{{ page.Title }}

@jmooring
Copy link
Member

jmooring commented Jan 1, 2022

The issue I remember with .Render is #8182 (comment), where it would be convenient to do something like:

{{ .Render "summary" (dict "page" .Page "myVar" "foo") }}

It doesn't seem like the proposed page function would address this particular case (myVar isn't on .Page). Please comment.

Also, what is the difference between page.Title and $.Title? It seems like both return the top level data context.

@bep
Copy link
Member Author

bep commented Jan 1, 2022

OK, I may have forgot what the ".Render problem" was ... but I suspect I'm talking about "another one".

Also, what is the difference between page.Title and $.Title? It seems like both return the top level data context.

So,

{{ $.Title }}
{{ page.Title }}

The above would only be the same in the entry template (e.g. single.html). The $ does not travel into partials and template constructs.

@jmooring
Copy link
Member

jmooring commented Jan 1, 2022

Does that mean I would be able to call a partial without passing the page context?

layouts/_default/single.html

{{ partial "render-some-page-stuff" }}

layouts/partials/render-some-page-stuff.html

Title: {{ page.Title }}<br>
Date: {{ page.Date }}<br>

@regisphilibert
Copy link
Member

We have site which is a invoking the page's .Site method throughout.

So it would make sense that this is called page.

I think this is great, the more simple the context passed to a partial is — and now we'll never need to pass the page — the better.

I don't foresee any problems. Except making sure users understand the fact that any template be it a partial a ".Render" or a returning partial are bound to a page and even though it has no consequence it might create confusion when using partialCached where your partial might end up with the same page. But that's just logical and only concerns users using the new feature.

@bep
Copy link
Member Author

bep commented Jan 1, 2022

Does that mean I would be able to call a partial without passing the page context?

Yes. But it's important to note that this is not only partials, and it would work in any nested situations, which is where I find the most pains in the current situation -- partial calling partial calling partial calling partial.

@bep
Copy link
Member Author

bep commented Jan 1, 2022

partialCached where your partial might end up with the same page.

That is true, but that is also a "problem" you would have today.

@bep bep changed the title Add render.Context (or something) template function Add page template function Jan 1, 2022
@bep bep self-assigned this Feb 18, 2022
bep added a commit to bep/hugo that referenced this issue Feb 25, 2022
@bep bep added Enhancement and removed Proposal labels Feb 15, 2023
@bep bep added this to the v0.111.0 milestone Feb 15, 2023
bep added a commit to bep/hugo that referenced this issue Feb 20, 2023
The main topic of this commit is that you can now index fragments (content heading identifiers) when calling `.Related`.

You can do this by:

* Configure one or more indices with type `fragments`
* The name of those index configurations maps to an (optional) front matter slice with fragment references. This allows you to link
page<->fragment and page<->page.
* This also will index all the fragments (heading identifiers) of the pages.
* When the match represents a Page fragment, then `.Fragment` will return non-nil.

A short template example:

```
{{ $related := site.RegularPages.Related . }}
{{ range $related }}
  {{ if .Fragment }}
  - {{ .Title }}: {{ .Fragment.Title }}:  {{ .Fragment.RelPermalink }}
  {{ else }}
  - {{ .Title }}: {{ .RelPermalink }}
  {{ end }}
{{ end }}
```

It's also possible to use type `fragments` indices in shortcode, e.g.:

```
{{ $related := site.RegularPages.Related .Page }}
```

But, and this is important, you need to include the shortcode using the `{{<` delimiter. Not doing so will create infinite loops and timeouts.

This commit also:

* Consolidates all `.Related*` methods into one, which takes either a `Page` or an options map as its only argument.
* Add `context.Context` to all of the content related Page API. Turns out it wasn't strictly needed for this particular feature, but it will
soon become usefil, e.g. in gohugoio#9339.

Closes gohugoio#10711
Updates gohugoio#9339
Updates gohugoio#10725
bep added a commit to bep/hugo that referenced this issue Feb 20, 2023
The main topic of this commit is that you can now index fragments (content heading identifiers) when calling `.Related`.

You can do this by:

* Configure one or more indices with type `fragments`
* The name of those index configurations maps to an (optional) front matter slice with fragment references. This allows you to link
page<->fragment and page<->page.
* This also will index all the fragments (heading identifiers) of the pages.
* When the match represents a Page fragment, then `.Fragment` will return non-nil.

A short template example:

```
{{ $related := site.RegularPages.Related . }}
{{ range $related }}
  {{ if .Fragment }}
  - {{ .Title }}: {{ .Fragment.Title }}:  {{ .Fragment.RelPermalink }}
  {{ else }}
  - {{ .Title }}: {{ .RelPermalink }}
  {{ end }}
{{ end }}
```

It's also possible to use type `fragments` indices in shortcode, e.g.:

```
{{ $related := site.RegularPages.Related .Page }}
```

But, and this is important, you need to include the shortcode using the `{{<` delimiter. Not doing so will create infinite loops and timeouts.

This commit also:

* Consolidates all `.Related*` methods into one, which takes either a `Page` or an options map as its only argument.
* Add `context.Context` to all of the content related Page API. Turns out it wasn't strictly needed for this particular feature, but it will
soon become usefil, e.g. in gohugoio#9339.

Closes gohugoio#10711
Updates gohugoio#9339
Updates gohugoio#10725
bep added a commit to bep/hugo that referenced this issue Feb 21, 2023
The main topic of this commit is that you can now index fragments (content heading identifiers) when calling `.Related`.

You can do this by:

* Configure one or more indices with type `fragments`
* The name of those index configurations maps to an (optional) front matter slice with fragment references. This allows you to link
page<->fragment and page<->page.
* This also will index all the fragments (heading identifiers) of the pages.

It's also possible to use type `fragments` indices in shortcode, e.g.:

```
{{ $related := site.RegularPages.Related .Page }}
```

But, and this is important, you need to include the shortcode using the `{{<` delimiter. Not doing so will create infinite loops and timeouts.

This commit also:

* Consolidates all `.Related*` methods into one, which takes either a `Page` or an options map as its only argument.
* Add `context.Context` to all of the content related Page API. Turns out it wasn't strictly needed for this particular feature, but it will
soon become usefil, e.g. in gohugoio#9339.

Closes gohugoio#10711
Updates gohugoio#9339
Updates gohugoio#10725
bep added a commit to bep/hugo that referenced this issue Feb 21, 2023
The main topic of this commit is that you can now index fragments (content heading identifiers) when calling `.Related`.

You can do this by:

* Configure one or more indices with type `fragments`
* The name of those index configurations maps to an (optional) front matter slice with fragment references. This allows you to link
page<->fragment and page<->page.
* This also will index all the fragments (heading identifiers) of the pages.

It's also possible to use type `fragments` indices in shortcode, e.g.:

```
{{ $related := site.RegularPages.Related .Page }}
```

But, and this is important, you need to include the shortcode using the `{{<` delimiter. Not doing so will create infinite loops and timeouts.

This commit also:

* Consolidates all `.Related*` methods into one, which takes either a `Page` or an options map as its only argument.
* Add `context.Context` to all of the content related Page API. Turns out it wasn't strictly needed for this particular feature, but it will
soon become usefil, e.g. in gohugoio#9339.

Closes gohugoio#10711
Updates gohugoio#9339
Updates gohugoio#10725
bep added a commit to bep/hugo that referenced this issue Feb 21, 2023
The main topic of this commit is that you can now index fragments (content heading identifiers) when calling `.Related`.

You can do this by:

* Configure one or more indices with type `fragments`
* The name of those index configurations maps to an (optional) front matter slice with fragment references. This allows you to link
page<->fragment and page<->page.
* This also will index all the fragments (heading identifiers) of the pages.

It's also possible to use type `fragments` indices in shortcode, e.g.:

```
{{ $related := site.RegularPages.Related .Page }}
```

But, and this is important, you need to include the shortcode using the `{{<` delimiter. Not doing so will create infinite loops and timeouts.

This commit also:

* Consolidates all `.Related*` methods into one, which takes either a `Page` or an options map as its only argument.
* Add `context.Context` to all of the content related Page API. Turns out it wasn't strictly needed for this particular feature, but it will
soon become usefil, e.g. in gohugoio#9339.

Closes gohugoio#10711
Updates gohugoio#9339
Updates gohugoio#10725
bep added a commit to bep/hugo that referenced this issue Feb 21, 2023
The main topic of this commit is that you can now index fragments (content heading identifiers) when calling `.Related`.

You can do this by:

* Configure one or more indices with type `fragments`
* The name of those index configurations maps to an (optional) front matter slice with fragment references. This allows you to link
page<->fragment and page<->page.
* This also will index all the fragments (heading identifiers) of the pages.

It's also possible to use type `fragments` indices in shortcode, e.g.:

```
{{ $related := site.RegularPages.Related .Page }}
```

But, and this is important, you need to include the shortcode using the `{{<` delimiter. Not doing so will create infinite loops and timeouts.

This commit also:

* Consolidates all `.Related*` methods into one, which takes either a `Page` or an options map as its only argument.
* Add `context.Context` to all of the content related Page API. Turns out it wasn't strictly needed for this particular feature, but it will
soon become usefil, e.g. in gohugoio#9339.

Closes gohugoio#10711
Updates gohugoio#9339
Updates gohugoio#10725
bep added a commit to bep/hugo that referenced this issue Feb 21, 2023
The main topic of this commit is that you can now index fragments (content heading identifiers) when calling `.Related`.

You can do this by:

* Configure one or more indices with type `fragments`
* The name of those index configurations maps to an (optional) front matter slice with fragment references. This allows you to link
page<->fragment and page<->page.
* This also will index all the fragments (heading identifiers) of the pages.

It's also possible to use type `fragments` indices in shortcode, e.g.:

```
{{ $related := site.RegularPages.Related .Page }}
```

But, and this is important, you need to include the shortcode using the `{{<` delimiter. Not doing so will create infinite loops and timeouts.

This commit also:

* Consolidates all `.Related*` methods into one, which takes either a `Page` or an options map as its only argument.
* Add `context.Context` to all of the content related Page API. Turns out it wasn't strictly needed for this particular feature, but it will
soon become usefil, e.g. in gohugoio#9339.

Closes gohugoio#10711
Updates gohugoio#9339
Updates gohugoio#10725
bep added a commit to bep/hugo that referenced this issue Feb 21, 2023
The main topic of this commit is that you can now index fragments (content heading identifiers) when calling `.Related`.

You can do this by:

* Configure one or more indices with type `fragments`
* The name of those index configurations maps to an (optional) front matter slice with fragment references. This allows you to link
page<->fragment and page<->page.
* This also will index all the fragments (heading identifiers) of the pages.

It's also possible to use type `fragments` indices in shortcode, e.g.:

```
{{ $related := site.RegularPages.Related .Page }}
```

But, and this is important, you need to include the shortcode using the `{{<` delimiter. Not doing so will create infinite loops and timeouts.

This commit also:

* Adds two new methods to Page: Fragments (can also be used to build ToC) and HeadingsFiltered (this is only used in Related Content with
index type `fragments` and `enableFilter` set to true.
* Consolidates all `.Related*` methods into one, which takes either a `Page` or an options map as its only argument.
* Add `context.Context` to all of the content related Page API. Turns out it wasn't strictly needed for this particular feature, but it will
soon become usefil, e.g. in gohugoio#9339.

Closes gohugoio#10711
Updates gohugoio#9339
Updates gohugoio#10725
bep added a commit to bep/hugo that referenced this issue Feb 21, 2023
The main topic of this commit is that you can now index fragments (content heading identifiers) when calling `.Related`.

You can do this by:

* Configure one or more indices with type `fragments`
* The name of those index configurations maps to an (optional) front matter slice with fragment references. This allows you to link
page<->fragment and page<->page.
* This also will index all the fragments (heading identifiers) of the pages.

It's also possible to use type `fragments` indices in shortcode, e.g.:

```
{{ $related := site.RegularPages.Related .Page }}
```

But, and this is important, you need to include the shortcode using the `{{<` delimiter. Not doing so will create infinite loops and timeouts.

This commit also:

* Adds two new methods to Page: Fragments (can also be used to build ToC) and HeadingsFiltered (this is only used in Related Content with
index type `fragments` and `enableFilter` set to true.
* Consolidates all `.Related*` methods into one, which takes either a `Page` or an options map as its only argument.
* Add `context.Context` to all of the content related Page API. Turns out it wasn't strictly needed for this particular feature, but it will
soon become usefil, e.g. in gohugoio#9339.

Closes gohugoio#10711
Updates gohugoio#9339
Updates gohugoio#10725
bep added a commit that referenced this issue Feb 21, 2023
The main topic of this commit is that you can now index fragments (content heading identifiers) when calling `.Related`.

You can do this by:

* Configure one or more indices with type `fragments`
* The name of those index configurations maps to an (optional) front matter slice with fragment references. This allows you to link
page<->fragment and page<->page.
* This also will index all the fragments (heading identifiers) of the pages.

It's also possible to use type `fragments` indices in shortcode, e.g.:

```
{{ $related := site.RegularPages.Related .Page }}
```

But, and this is important, you need to include the shortcode using the `{{<` delimiter. Not doing so will create infinite loops and timeouts.

This commit also:

* Adds two new methods to Page: Fragments (can also be used to build ToC) and HeadingsFiltered (this is only used in Related Content with
index type `fragments` and `enableFilter` set to true.
* Consolidates all `.Related*` methods into one, which takes either a `Page` or an options map as its only argument.
* Add `context.Context` to all of the content related Page API. Turns out it wasn't strictly needed for this particular feature, but it will
soon become usefil, e.g. in #9339.

Closes #10711
Updates #9339
Updates #10725
@bep bep mentioned this issue Feb 25, 2023
18 tasks
bep added a commit to bep/hugo that referenced this issue Feb 25, 2023
bep added a commit to bep/hugo that referenced this issue Feb 25, 2023
bep added a commit to bep/hugo that referenced this issue Feb 25, 2023
bep added a commit to bep/hugo that referenced this issue Feb 25, 2023
bep added a commit to bep/hugo that referenced this issue Feb 25, 2023
bep added a commit to bep/hugo that referenced this issue Feb 25, 2023
bep added a commit to bep/hugo that referenced this issue Feb 25, 2023
bep added a commit to bep/hugo that referenced this issue Feb 25, 2023
bep added a commit that referenced this issue Feb 25, 2023
@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 19, 2023
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.

3 participants