home-assistant.io icon indicating copy to clipboard operation
home-assistant.io copied to clipboard

Improve jinja macro examples

Open flaterichd opened this issue 1 year ago • 0 comments

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

flaterichd avatar Feb 16 '24 10:02 flaterichd