| 1 |
--- |
| 2 |
title: Render |
| 3 |
description: Renders a view 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 VIEW] |
| 10 |
aliases: [/functions/render] |
| 11 |
--- |
| 12 |
|
| 13 |
The `Render` method on a `Page` object renders a [view template][] with the given page as context. |
| 14 |
|
| 15 |
{{< new-in 0.164.0 >}} |
| 16 |
The `VIEW` argument now supports slash-separated directory paths. |
| 17 |
{{< /new-in >}} |
| 18 |
|
| 19 |
The `VIEW` argument is the name of a _view_ template, optionally preceded by a slash-separated directory path. Do not include a file extension. Hugo resolves the template via the [template lookup order][], so the same `VIEW` value may map to different _view_ templates depending on the page being rendered. |
| 20 |
|
| 21 |
Consider this layout structure: |
| 22 |
|
| 23 |
```tree |
| 24 |
layouts/ |
| 25 |
├── books/ |
| 26 |
│ └── summary.html |
| 27 |
├── baseof.html |
| 28 |
├── home.html |
| 29 |
├── page.html |
| 30 |
├── section.html |
| 31 |
├── summary.html |
| 32 |
├── taxonomy.html |
| 33 |
└── term.html |
| 34 |
``` |
| 35 |
|
| 36 |
And this template: |
| 37 |
|
| 38 |
```go-html-template |
| 39 |
<ul> |
| 40 |
{{ range site.RegularPages }} |
| 41 |
{{ .Render "summary" }} |
| 42 |
{{ end }} |
| 43 |
</ul> |
| 44 |
``` |
| 45 |
|
| 46 |
When rendering content of type `books`, the `Render` method calls: |
| 47 |
|
| 48 |
```text |
| 49 |
layouts/books/summary.html |
| 50 |
``` |
| 51 |
|
| 52 |
For all other pages, the `Render` method calls: |
| 53 |
|
| 54 |
```text |
| 55 |
layouts/summary.html |
| 56 |
``` |
| 57 |
|
| 58 |
As a best practice, place _view_ templates together in a dedicated subdirectory. Hugo does not reserve a directory name for _view_ templates as it does for `_partials`, `_shortcodes`, and `_markup`. The examples below use `_views`, where the underscore prefix differentiates it from other path segments and conveys its purpose, but a directory named `foo` would work equally well. |
| 59 |
|
| 60 |
With path segments, consider this layout structure: |
| 61 |
|
| 62 |
```tree |
| 63 |
layouts/ |
| 64 |
├── _views/ |
| 65 |
│ └── summary.html |
| 66 |
├── books/ |
| 67 |
│ └── _views/ |
| 68 |
│ └── summary.html |
| 69 |
├── baseof.html |
| 70 |
├── home.html |
| 71 |
├── page.html |
| 72 |
├── section.html |
| 73 |
├── taxonomy.html |
| 74 |
└── term.html |
| 75 |
``` |
| 76 |
|
| 77 |
And this template: |
| 78 |
|
| 79 |
```go-html-template |
| 80 |
<ul> |
| 81 |
{{ range site.RegularPages }} |
| 82 |
{{ .Render "_views/summary" }} |
| 83 |
{{ end }} |
| 84 |
</ul> |
| 85 |
``` |
| 86 |
|
| 87 |
When rendering content of type `books`, the `Render` method calls: |
| 88 |
|
| 89 |
```text |
| 90 |
layouts/books/_views/summary.html |
| 91 |
``` |
| 92 |
|
| 93 |
For all other pages, the `Render` method calls: |
| 94 |
|
| 95 |
```text |
| 96 |
layouts/_views/summary.html |
| 97 |
``` |
| 98 |
|
| 99 |
Although similar to the [`partial`][] function, there are key differences. |
| 100 |
|
| 101 |
`Render` method|`partial` function |
| 102 |
:--|:-- |
| 103 |
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. |
| 104 |
The template is resolved automatically via the [template lookup order][].|You must specify the template name, relative to the `layouts/_partials` directory. |
| 105 |
|
| 106 |
[`partial`]: /functions/partials/include/ |
| 107 |
[template lookup order]: /templates/lookup-order/ |
| 108 |
[view template]: /templates/types/#view |