Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global macros? #32

Open
jlvaillant opened this issue Oct 1, 2021 · 4 comments
Open

Global macros? #32

jlvaillant opened this issue Oct 1, 2021 · 4 comments

Comments

@jlvaillant
Copy link

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...

@AngellusMortis
Copy link

+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 %}

@shbatm
Copy link

shbatm commented May 1, 2022

Would also like to see this.

@dajomas
Copy link

dajomas commented Dec 21, 2022

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

@romainchassaigne
Copy link

romainchassaigne commented Nov 12, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants