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