Table render hooks
Context
Table render hook templates receive the following context:
Attributes
(map
) The Markdown attributes, available if you configure your site as follows:
markup:
goldmark:
parser:
attribute:
block: true
[markup]
[markup.goldmark]
[markup.goldmark.parser]
[markup.goldmark.parser.attribute]
block = true
{
"markup": {
"goldmark": {
"parser": {
"attribute": {
"block": true
}
}
}
}
}
Ordinal
(int
) The zero-based ordinal of the table on the page.
Page
(page
) A reference to the current page.
PageInner
(page
) A reference to a page nested via the RenderShortcodes
method. See details.
Position
(string
) The position of the table within the page content.
THead
(slice
) A slice of table header rows, where each element is a slice of table cells.
TBody
(slice
) A slice of table body rows, where each element is a slice of table cells.
Table cells
Each table cell within the slice of slices returned by the THead
and TBody
methods has the following fields:
Alignment
(string
) The alignment of the text within the table cell, one of left
, center
, or right
.
Text
(template.HTML
) The text within the table cell.
Example
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:
<table
{{- range $k, $v := .Attributes }}
{{- if $v }}
{{- printf " %s=%q" $k $v | safeHTMLAttr }}
{{- end }}
{{- end }}>
<thead>
{{- range .THead }}
<tr>
{{- range . }}
<th
{{- with .Alignment }}
{{- printf " style=%q" (printf "text-align: %s" .) | safeHTMLAttr }}
{{- end -}}
>
{{- .Text -}}
</th>
{{- end }}
</tr>
{{- end }}
</thead>
<tbody>
{{- range .TBody }}
<tr>
{{- range . }}
<td
{{- with .Alignment }}
{{- printf " style=%q" (printf "text-align: %s" .) | safeHTMLAttr }}
{{- end -}}
>
{{- .Text -}}
</td>
{{- end }}
</tr>
{{- end }}
</tbody>
</table>
PageInner details
New in v0.125.0The primary use case for PageInner
is to resolve links and page resources relative to an included Page
. For example, create an “include” shortcode to compose a page from multiple content files, while preserving a global context for footnotes and the table of contents:
{{ with .Get 0 }}
{{ with $.Page.GetPage . }}
{{- .RenderShortcodes }}
{{ else }}
{{ errorf "The %q shortcode was unable to find %q. See %s" $.Name . $.Position }}
{{ end }}
{{ else }}
{{ errorf "The %q shortcode requires a positional parameter indicating the logical path of the file to include. See %s" .Name .Position }}
{{ end }}
Then call the shortcode in your Markdown:
{{% include "/posts/p2" %}}
Any render hook triggered while rendering /posts/p2
will get:
/posts/p1
when callingPage
/posts/p2
when callingPageInner
PageInner
falls back to the value of Page
if not relevant, and always returns a value.
As a practical example, Hugo’s embedded link and image render hooks use the PageInner
method to resolve markdown link and image destinations. See the source code for each: