Debian Codegraph @

docs/content/en/methods/page/Render.md

raw
1 ---
2 title: Render
3 description: Returns the result of rendering a view template with the given page as context, or with an optional context argument.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: template.HTML
9 signatures: ['PAGE.Render VIEW [CONTEXT]']
10 aliases: [/functions/render]
11 ---
12
13 The `Render` method on a `Page` object renders a [view template][] with the given page as [context](g), or with an optional context argument.
14
15 {{< new-in 0.164.0 >}}
16 The `VIEW` argument now supports slash-separated directory paths.
17 {{< /new-in >}}
18
19 {{< new-in 0.166.0 >}}
20 This method now accepts an optional `CONTEXT` argument.
21 {{< /new-in >}}
22
23 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 templates depending on the page being rendered.
24
25 By default, Hugo passes the `Page` object as the context (the dot) when rendering the template. To pass a different context, provide the optional `CONTEXT` argument.
26
27 ## Examples
28
29 The following examples demonstrate calling this method with and without a custom context argument.
30
31 ### Default context
32
33 When called without a context argument, the `Page` object is the context within the template:
34
35 ```go-html-template {file="layouts/home.html"}
36 <ul>
37 {{ range site.RegularPages }}
38 <li>{{ .Render "_views/summary" }}</li>
39 {{ end }}
40 </ul>
41 ```
42
43 ```go-html-template {file="layouts/_views/summary.html"}
44 <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
45 ```
46
47 ### Custom context
48
49 To pass additional data to a view template, provide a custom context argument. This example passes a map as context, combining the `Page` object with an additional key-value pair:
50
51 ```go-html-template {file="layouts/home.html"}
52 <div>
53 {{ range site.RegularPages }}
54 {{ .Render "_views/card" (dict "page" . "class" "featured") }}
55 {{ end }}
56 </div>
57 ```
58
59 ```go-html-template {file="layouts/_views/card.html"}
60 <div class="card {{ .class }}">
61 <h2><a href="{{ .page.RelPermalink }}">{{ .page.LinkTitle }}</a></h2>
62 {{ .page.Summary }}
63 </div>
64 ```
65
66 ## Organization
67
68 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.
69
70 The following example uses path segments to organize _view_ templates in a dedicated subdirectory:
71
72 ```tree
73 layouts/
74 ├── _views/
75 │ └── summary.html
76 ├── books/
77 │ └── _views/
78 │ └── summary.html
79 ├── baseof.html
80 ├── home.html
81 ├── page.html
82 ├── section.html
83 ├── taxonomy.html
84 └── term.html
85 ```
86
87 And this template:
88
89 ```go-html-template {file="layouts/home.html"}
90 <ul>
91 {{ range site.RegularPages }}
92 {{ .Render "_views/summary" }}
93 {{ end }}
94 </ul>
95 ```
96
97 When rendering content of type `books`, the `Render` method calls:
98
99 ```text
100 layouts/books/_views/summary.html
101 ```
102
103 For all other pages, the `Render` method calls:
104
105 ```text
106 layouts/_views/summary.html
107 ```
108
109 ## Notes
110
111 Although similar to the [`partial`][] function, there are key differences.
112
113 {{% include "/_common/render-vs-partial.md" %}}
114
115 [`partial`]: /functions/partials/include/
116 [template lookup order]: /templates/lookup-order/
117 [view template]: /templates/types/#view