home-assistant / home-assistant.io

:blue_book: Home Assistant User documentation
https://www.home-assistant.io
Other
4.58k stars 7.11k forks source link

Improve jinja macro examples #31461

Open flaterichd opened 5 months ago

flaterichd commented 5 months ago

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

github-actions[bot] commented 3 months ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved. If this issue is still relevant, please let us know by leaving a comment 👍 This issue has now has been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.