home-assistant.io
home-assistant.io copied to clipboard
Improve jinja macro examples
Feedback
The deault example causes macro definitions and imports to add weird white spaces and line breaks, which clutter "Developer Tools / Template" usage and potentialy can cause difficulties in calling macros in some cases, where white spaces are not expected.
Jinja docs state "You can also strip whitespace in templates by hand. If you add a minus sign (-) to the start or end of a block (e.g. a tag), a comment, or a variable expression, the whitespaces before or after that block will be removed:".
This solved white space issues for me, which I had after copying examples from HA docs. So I propose that HA documentation is also improved for beginners.
{%- macro format_entity(entity_id) -%}
{{ state_attr(entity_id, 'friendly_name') }} - {{ states(entity_id) }}
{%- endmacro -%}
{%- from 'formatter.jinja' import format_entity -%}
{{ format_entity('sensor.temperature') }}
URL
https://www.home-assistant.io/docs/configuration/templating/
Version
2024.2.1
Additional information
No response