| 1 |
--- |
| 2 |
title: Render |
| 3 |
description: Renders the given template with the given page as context. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: template.HTML |
| 9 |
signatures: [PAGE.Render NAME] |
| 10 |
aliases: [/functions/render] |
| 11 |
--- |
| 12 |
|
| 13 |
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. |
| 14 |
|
| 15 |
```go-html-template |
| 16 |
{{ range site.RegularPages }} |
| 17 |
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2> |
| 18 |
{{ .Render "summary" }} |
| 19 |
{{ end }} |
| 20 |
``` |
| 21 |
|
| 22 |
In the example above, note that the template ("summary") is identified by its file name without directory or extension. |
| 23 |
|
| 24 |
Although similar to the [`partial`][] function, there are key differences. |
| 25 |
|
| 26 |
`Render` method|`partial` function |
| 27 |
:--|:-- |
| 28 |
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. |
| 29 |
The path to the template is determined by the [content type](g).|You must specify the path to the template, relative to the `layouts/_partials` directory. |
| 30 |
|
| 31 |
Consider this layout structure: |
| 32 |
|
| 33 |
```tree |
| 34 |
layouts/ |
| 35 |
├── books/ |
| 36 |
│ └── li.html <-- used when content type is "books" |
| 37 |
├── baseof.html |
| 38 |
├── home.html |
| 39 |
├── li.html <-- used for other content types |
| 40 |
├── page.html |
| 41 |
├── section.html |
| 42 |
├── taxonomy.html |
| 43 |
└── term.html |
| 44 |
``` |
| 45 |
|
| 46 |
And this template: |
| 47 |
|
| 48 |
```go-html-template |
| 49 |
<ul> |
| 50 |
{{ range site.RegularPages.ByDate }} |
| 51 |
{{ .Render "li" }} |
| 52 |
{{ end }} |
| 53 |
</ul> |
| 54 |
``` |
| 55 |
|
| 56 |
When rendering content of type "books" the `Render` method calls: |
| 57 |
|
| 58 |
```text |
| 59 |
layouts/books/li.html |
| 60 |
``` |
| 61 |
|
| 62 |
For all other content types the `Render` methods calls: |
| 63 |
|
| 64 |
```text |
| 65 |
layouts/li.html |
| 66 |
``` |
| 67 |
|
| 68 |
See [content views][] for more examples. |
| 69 |
|
| 70 |
[`partial`]: /functions/partials/include/ |
| 71 |
[content views]: /templates/types/#content-view |