Debian Codegraph @

docs/content/en/render-hooks/tables.md

raw
1 ---
2 title: Table render hooks
3 linkTitle: Tables
4 description: Create table render hook templates to override the rendering of Markdown tables to HTML.
5 categories: []
6 keywords: []
7 ---
8
9 {{< new-in 0.134.0 />}}
10
11 ## Context
12
13 Table _render hook_ templates receive the following [context](g):
14
15 `Attributes`
16 : (`map`) The [Markdown attributes][], available if you configure your site as follows:
17
18 {{< code-toggle file=hugo >}}
19 [markup.goldmark.parser.attribute]
20 block = true
21 {{< /code-toggle >}}
22
23 `Ordinal`
24 : (`int`) The zero-based ordinal of the table on the page.
25
26 `Page`
27 : (`page`) A reference to the current page.
28
29 `PageInner`
30 : (`page`) A reference to a page nested via the [`RenderShortcodes`][] method. [See details](#pageinner-details).
31
32 `Position`
33 : (`string`) The position of the table within the page content.
34
35 `THead`
36 : (`slice`) A slice of table header rows, where each element is a slice of table cells.
37
38 `TBody`
39 : (`slice`) A slice of table body rows, where each element is a slice of table cells.
40
41 ## Table cells
42
43 Each table cell within the slice of slices returned by the `THead` and `TBody` methods has the following fields:
44
45 `Alignment`
46 : (`string`) The alignment of the text within the table cell, one of `left`, `center`, or `right`.
47
48 `Text`
49 : (`template.HTML`) The text within the table cell.
50
51 ## Example
52
53 In its default configuration, Hugo renders Markdown tables according to the [GitHub Flavored Markdown specification][]. To create a render hook that does the same thing:
54
55 ```go-html-template {file="layouts/_markup/render-table.html" copy=true}
56 <table
57 {{- range $k, $v := .Attributes }}
58 {{- if $v }}
59 {{- printf " %s=%q" $k $v | safeHTMLAttr }}
60 {{- end }}
61 {{- end }}>
62 <thead>
63 {{- range .THead }}
64 <tr>
65 {{- range . }}
66 <th
67 {{- with .Alignment }}
68 {{- printf " style=%q" (printf "text-align: %s" .) | safeHTMLAttr }}
69 {{- end -}}
70 >
71 {{- .Text -}}
72 </th>
73 {{- end }}
74 </tr>
75 {{- end }}
76 </thead>
77 <tbody>
78 {{- range .TBody }}
79 <tr>
80 {{- range . }}
81 <td
82 {{- with .Alignment }}
83 {{- printf " style=%q" (printf "text-align: %s" .) | safeHTMLAttr }}
84 {{- end -}}
85 >
86 {{- .Text -}}
87 </td>
88 {{- end }}
89 </tr>
90 {{- end }}
91 </tbody>
92 </table>
93 ```
94
95 {{% include "/_common/render-hooks/pageinner.md" %}}
96
97 [GitHub Flavored Markdown specification]: https://github.github.com/gfm/#tables-extension-
98 [Markdown attributes]: /content-management/markdown-attributes/
99 [`RenderShortcodes`]: /methods/page/rendershortcodes/