Skip to content
Open
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ae364aa
ListItem Render Hook
alexanderhansen Apr 26, 2022
acc3263
Update Tests
alexanderhansen May 2, 2022
4a659ee
Merge branch 'gohugoio:master' into listitem-render-hook
alexanderhansen May 2, 2022
106caac
ListItem Render Hook
alexanderhansen Apr 26, 2022
e751003
Update Tests
alexanderhansen May 2, 2022
b143fa6
Merge branch 'listitem-render-hook' of https://github.com/alexanderha…
alexanderhansen May 2, 2022
5e0eba9
List Render Hook
alexanderhansen May 2, 2022
5130a3d
With ordered parameter
alexanderhansen May 2, 2022
c025e3f
ListItem has no attributes
alexanderhansen May 2, 2022
156e671
Test for attributes
alexanderhansen May 2, 2022
5757d1d
First and Last sibling check for listitems
alexanderhansen May 5, 2022
1c954b9
Borrowed from Goldmark
alexanderhansen May 5, 2022
0de16a5
ListItem Render Hook
alexanderhansen Apr 26, 2022
b04f772
Update Tests
alexanderhansen May 2, 2022
595c696
List Render Hook
alexanderhansen May 2, 2022
9d3616a
With ordered parameter
alexanderhansen May 2, 2022
5e38325
ListItem has no attributes
alexanderhansen May 2, 2022
6ccad18
Test for attributes
alexanderhansen May 2, 2022
433d9cf
First and Last sibling check for listitems
alexanderhansen May 5, 2022
600e5a5
Borrowed from Goldmark
alexanderhansen May 5, 2022
4266674
Merge branch 'listitem-render-hook' of https://github.com/alexanderha…
alexanderhansen May 5, 2022
9199981
docs
alexanderhansen May 5, 2022
db2a4ca
Delete devcontainer.json
alexanderhansen May 5, 2022
3cb278b
Merge branch 'gohugoio:master' into listitem-render-hook
alexanderhansen Aug 3, 2022
c85708b
Merge branch 'gohugoio:master' into listitem-render-hook
alexanderhansen Dec 8, 2022
398be2d
Merge branch 'gohugoio:master' into listitem-render-hook
alexanderhansen Apr 6, 2023
3b2b6b1
Add Context to Renderer
alexanderhansen Apr 6, 2023
967447f
Merge branch 'gohugoio:master' into listitem-render-hook
alexanderhansen Apr 14, 2023
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs
  • Loading branch information
alexanderhansen committed May 5, 2022
commit 9199981de8a75e6b27f1e8179ae8687181ff3376
97 changes: 97 additions & 0 deletions docs/content/en/templates/render-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ The hook kinds currently supported are:
* `link`
* `heading` {{< new-in "0.71.0" >}}
* `codeblock`{{< new-in "0.93.0" >}}
* `list`{{< new-in "0.99.0" >}}
* `listitem`{{< new-in "0.99.0" >}}

You can define [Output-Format-](/templates/output-formats) and [language-](/content-management/multilingual/)specific templates if needed. Your `layouts` folder may look like this:

Expand Down Expand Up @@ -170,3 +172,98 @@ Page

Position
: Useful in error logging as it prints the filename and position (linenumber, column), e.g. `{{ errorf "error in code block: %s" .Position }}`.

## Render Hooks for Lists and List Items

{{< new-in "0.99.0" >}}

You can add a hook template for lists and list items e.g. for different output types

```goat { class="black f7" }
layouts
└── _default
└── _markup
└── render-listitem.html
└── render-listitem.json
└── render-list.html
└── render-list.json
```

The `render-list` template will receive this context:

Page
: The [Page](/variables/page/) being rendered.

Text
: The rendered (HTML) list content.

PlainText
: The plain variant of the above.

IsOrdered (bool)
: If this is an ordered list.

Parent
: The Parent node of the list.

Attributes (map) {{< new-in "0.82.0" >}}
: A map of attributes (e.g. `id`, `class`)


The `render-listitem` template will receive this context:

Page
: The [Page](/variables/page/) being rendered.

Text
: The rendered (HTML) list content.

PlainText
: The plain variant of the above.

IsFirst (bool)
: If this is the first item in the list.

IsLast (bool)
: If this is the last item in the list.

Parent
: The Parent node of the item, the list node.

### ListItem rendered as JSON-LD example:

```md
1. Do This
2. Then That
```

Here is a code example for how the render-listitem.json template could look:

{{< code file="layouts/_default/_markup/render-list.html" >}}
{{- if eq .Parent.IsOrdered true -}}
{
"@type": "HowToStep",
"text": "{{ .Text | plainify}}"
}{{ if not .IsLast }},{{ end }}{{ print "\n"}}
{{- else -}}
{{- if not .IsLast -}}
{{ printf "\"%s\",\n" .Text | plainify}}
{{- else -}}
{{ printf "\"%s\"" .Text | plainify}}
{{- end -}}
{{- end -}}
{{< /code >}}


The rendered html will be

```js
{
"@type": "HowToStep",
"text": "Do This"
},
{
"@type": "HowToStep",
"text": "Then That"
}
```