Enable jinja loop controls (break/continue) (#88625)

Enables jinja loop controls (break/continue)
This commit is contained in:
David Poll 2023-02-23 19:14:28 -08:00 committed by GitHub
parent 9575cd9161
commit af49b98475
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -2063,6 +2063,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
self.template_cache: weakref.WeakValueDictionary[
str | jinja2.nodes.Template, CodeType | str | None
] = weakref.WeakValueDictionary()
self.add_extension("jinja2.ext.loopcontrols")
self.filters["round"] = forgiving_round
self.filters["multiply"] = multiply
self.filters["log"] = logarithm

View File

@ -243,6 +243,26 @@ def test_iterating_domain_states(hass: HomeAssistant) -> None:
)
def test_loop_controls(hass: HomeAssistant) -> None:
"""Test that loop controls are enabled."""
assert (
template.Template(
"""
{%- for v in range(10) %}
{%- if v == 1 -%}
{%- continue -%}
{%- elif v == 3 -%}
{%- break -%}
{%- endif -%}
{{ v }}
{%- endfor -%}
""",
hass,
).async_render()
== "02"
)
def test_float_function(hass: HomeAssistant) -> None:
"""Test float function."""
hass.states.async_set("sensor.temperature", "12")