Open jlvaillant opened 2 years ago
+1 for this. There is really only 3 marcos I would really love to be global. Basically being able to recursively print an object. If you just do {{ object }}
and object
is a mapping, it does not work quite as expected.
{% macro print_string(ws, item) -%}
{%- if "\n" in item -%} |
{{" "*ws}}{{ item|replace("\n", "\n" + " "*ws) }}
{%- else -%}
"{{ item }}"
{%- endif -%}
{%- endmacro %}
{% macro print_item(ws, item) -%}
{%- if item is mapping -%}
{{ print_dict(ws, item) }}
{%- elif item is iterable and item is not string -%}
{{ print_list(ws, item) }}
{%- elif item is string -%}
{{ print_string(ws, item) }}
{%- elif item == None -%}
{%- else -%}
{{ item }}
{%- endif -%}
{%- endmacro %}
{% macro print_dict(ws, items) -%}
{%- for key, value in items.items() -%}
{%- if value is mapping or (value is iterable and value is not string) -%}
{% if loop.index > 1 %}{{" "*ws}}{% endif %}{{ key }}:
{{" "*ws}} {{ print_item(ws + 1, value) }}
{% else -%}
{% if loop.index > 1 %}{{" "*ws}}{% endif %}{{ key }}: {{ print_item(ws + 1, value) }}
{% endif -%}
{% endfor -%}
{%- endmacro %}
{% macro print_list(ws, items) -%}
{%- for item in items -%}
{% if loop.index > 1 %}{{" "*ws}}{% endif %}- {{ print_item(ws + 1, item) }}
{% endfor -%}
{%- endmacro %}
Would also like to see this.
Recently discovered this gem, lovelace_gen, and I am using it avidly. However, I use several include files that contain the same macros. So I would love to see something like global macros or macro include files to minimize the size of the yaml files and the need to change multiple files if I adjust a macro that is used in multiple files.
So hereby, I add my +1 for this request
Hey there,
I encountered the same issue, and after doing some research, I found that creating Jinja templates like in Flask's was impossible in this context.
Here's how I managed to resolve the issue. It's a bit of a workaround, but it got the job done.
Note about the files: The white space might not be perfectly aligned as I copied this from my lovelace, which has cards in both vertical and horizontal stacks.
./file_1.yaml
please see edit to a better way:
# lovelace_gen
{% set room_chips = [("sensor.temperature", "mdi:thermometer", "state"),
("sensor.humidity", "mdi:water-percent", "state")] %}
title: Test
path: test
cards:
- !include
- "include/chips.yaml"
- room:
{% for entity, icon, primary_info in desk_room_chips %}
- - {{entity}}
- {{icon}}
- {{primary_info}}
{% endfor %}
ws: 1
./include/chips.yaml
:
# lovelace_gen
{{" "*ws}}type: custom:mushroom-chips-card
{{" "*ws}}chips:
{{" "*ws}} {% for entity, icon, primary_info in room %}
{{" "*ws}} - type: entity
{{" "*ws}} entity: {{ entity }}
{{" "*ws}} icon: {{ icon }}
{{" "*ws}} content_info: {{ primary_info }}
{{" "*ws}} tap_action:
{{" "*ws}} action: none
{{" "*ws}} hold_action:
{{" "*ws}} action: none
{{" "*ws}} double_tap_action:
{{" "*ws}} action: none
{{" "*ws}} {% endfor %}
It might look a bit messy, but it served as a temporary solution until I find a cleaner approach.
EDIT: After reading the documentation, I found a better way to call my included file with json variables:
./file_1.yaml
:
# lovelace_gen
{% set room_chips = [("sensor.temperature", "mdi:thermometer", "state"),
("sensor.humidity", "mdi:water-percent", "state")] %}
title: Test
path: test
cards:
- !include
- "include/chips.yaml"
- room: {{ room_chips | tojson }}
ws: 1
Hi Thomas,
is there a chance global macros could be supported? I remember a pull request by Rocka84 for the "old" Lovelace_gen.py project that was a great attempt IMHO. Global macros would totally be a game changer for me...