aboutsummaryrefslogtreecommitdiffhomepage
path: root/docs/content/en/methods/page/Render.md
blob: bc3f58352c0895a20d3b3d570e13dfc56a74c584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
title: Render
description: Renders the given template with the given page as context.
categories: []
keywords: []
action:
  related:
    - functions/partials/Include
    - functions/partials/IncludeCached
  returnType: template.HTML
  signatures: [PAGE.Render NAME]
aliases: [/functions/render]
---

Typically used when ranging over a page collection, the `Render` method on a `Page` object renders the given template, passing the given page as context.

```go-html-template
{{ range site.RegularPages }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
  {{ .Render "summary" }}
{{ end }}
```

In the example above, note that the template ("summary") is identified by its file name without directory or extension.

Although similar to the [`partial`] function, there are key differences.

`Render` method|`partial` function|
:--|:--
The `Page` object is automatically passed to the given template. You cannot pass additional context.| You must specify the context, allowing you to pass a combination of objects, slices, maps, and scalars.
The path to the template is determined by the [content type].|You must specify the path to the template, relative to the layouts/partials directory.

Consider this layout structure:

```text
layouts/
├── _default/
│   ├── baseof.html
│   ├── home.html
│   ├── li.html      <-- used for other content types
│   ├── list.html
│   ├── single.html
│   └── summary.html
└── books/
    ├── li.html      <-- used when content type is "books"
    └── summary.html
```

And this template:

```go-html-template
<ul>
  {{ range site.RegularPages.ByDate }}
    {{ .Render "li" }}
  {{ end }}
</ul>
```

When rendering content of type "books" the `Render` method calls:

```text
layouts/books/li.html
```

For all other content types the `Render` methods calls:

```text
layouts/_default/li.html
```

See [content views] for more examples.

[content views]: /templates/views
[`partial`]: /functions/partials/include
[content type]: /getting-started/glossary/#content-type