custom-cards / button-card

❇️ Lovelace button-card for home assistant
MIT License
1.93k stars 233 forks source link

Support the new template macros in HA 2023.4 #682

Closed shlomki closed 1 year ago

shlomki commented 1 year ago

Home Assistant 2023.4 adds new functionality for reusing jinja templates by writing macros: https://www.home-assistant.io/docs/configuration/templating/#reusing-templates

However, I wasn't able to use this functionality in button-card.

I've tried adding the macro in the example to HA, and by writing the following button card code (though I'm not sure the syntax is correct here):

type: custom:button-card
name: | 
  [[[ import "answer_question" from "formatter.jinja"; return answer_question('light.kitchen'); ]]]

But got the following error:

ButtonCardJSTemplateError: SyntaxError: Cannot use import statement outside a module in 'import "answer_question" from "formatter.jinja"; return answer_question('light.kitchen');'
    at new Function (<anonymous>)

it seems that the problem is with javascript and how it tries to import modules and not macros. It would be really nice to be able to use macros in button-card, do you think you would be able to add support for it? Would gladly help in any way I can.

Is your feature request related to a problem? Please describe. Described above.

Describe the solution you'd like Be able to use macros in some way in button-card templates.

Describe alternatives you've considered Writing the same template over and over again every time it needs to be used.

Mariusthvdb commented 1 year ago

its simple: button-card uses javascript, not jinja. so forget about those custom_templates.

as for system wide templates in button-card: we've had those since forever through configuration templates.

please close the issue @shlomki