brunosabot / streamline-card

Streamline your Lovelace configuration with with a card template system.
MIT License
14 stars 2 forks source link

JavaScript templates cannot handle non-primitive variables #10

Closed anthonyma94 closed 14 hours ago

anthonyma94 commented 1 week ago

Describe the bug
When using JavaScript templates, referencing non-primitive variables (objects & arrays) will result in errors. The error depends on the parser, but nothing will act as expected.

To Reproduce With these variables:

default:
  - card:
      type: custom:mushroom-title-card
      title: hello
  - cards:
    - type: custom:mushroom-title-card
      title: hello

This works:

card:
  type: vertical-stack
  cards:
    - "[[card]]"
  # or
  cards: "[[cards]]"

But this doesn't:

card:
  type: vertical-stack
  cards_javascript: "['[[card]]']"
  # or
  cards_javascript: "'[[cards]]'"

Expected behavior I expect both options to work.

brunosabot commented 1 week ago

Hello @anthonyma94,

It's hopefully fixed in version 0.0.7 released this morning. Could you tell me if it's OK for you?

anthonyma94 commented 1 week ago

Thanks for the quick response! I'm getting a different error for objects: Uncaught (in promise) SyntaxError: unexpected token: ':'. Arrays seem to be fine with my limited testing.

default:
  - testing:
      text: hello
card:
  type: custom:mushroom-title-card
  title_javascript: "'[[testing]]'.text"
brunosabot commented 1 week ago

That's normal, this is not valid javascript: it transposes to: {"text":"hello"}.text which is invalid in JavaScript. You can test in the console. If you have such need, you probably need this template:

default:
  - testing:
      text: hello
card:
  type: custom:mushroom-title-card
  title_javascript: "('[[testing]]').text"

Basically, the parenthesis will instantiate the object for real and then you are able to get its properties.

brunosabot commented 14 hours ago

Closing as resolved. Do not hesitate to reopen the issue if necessary.