From 3e11933f99883855255d545d28cf02e024ece3ef Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sun, 9 Mar 2025 14:01:30 +0100 Subject: [PATCH] Add template function: flatten (#37879) --- .../_docs/configuration/templating.markdown | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/source/_docs/configuration/templating.markdown b/source/_docs/configuration/templating.markdown index a47d5992ef2..59bf5d3d6bf 100644 --- a/source/_docs/configuration/templating.markdown +++ b/source/_docs/configuration/templating.markdown @@ -1207,6 +1207,30 @@ Some examples: {% endraw %} +### Flatten a list of lists + +The template engine provides a filter to flatten a list of lists: `flatten`. + +It will take a list of lists and return a single list with all the elements. +The depth of the flattening can be controlled using the `levels` parameter. +The flattening process is recursive, so it will flatten all nested lists, until +the number of levels (if specified) is reached. + +Some examples: + +{% raw %} + +- `{{ flatten([1, [2, [3]], 4, [5 , 6]]) }}` - renders as `[1, 2, 3, 4, 5, 6]` +- `{{ [1, [2, [3]], 4, [5 , 6]] | flatten }}` - renders as `[1, 2, 3, 4, 5, 6]` + +- `{{ flatten([1, [2, [3]]], levels=1) }}` - renders as `[1, 2, [3]]` +- `{{ [1, [2, [3]]], flatten(levels=1) }}` - renders as `[1, 2, [3]]` + +- `{{ flatten([1, [2, [3]]], 1) }}` - renders as `[1, 2, [3]]` +- `{{ [1, [2, [3]]], flatten(1) }}` - renders as `[1, 2, [3]]` + +{% endraw %} + ## Merge action responses Using action responses we can collect information from various entities at the same time.