thomasloven / hass-lovelace_gen

🔹 Improve the lovelace yaml parser for Home Assistant
MIT License
209 stars 22 forks source link

[Suggestion] Change Jinja Escape Character #18

Open Matchlighter opened 3 years ago

Matchlighter commented 3 years ago

I've been dealing with a number of usability issues when trying to use Home Assistant xyz_template keys in Lovelace. I feel like {% raw %} and {{ '{{' }} are very ugly and verbose with as much as I'm trying to use them. I was looking at the Jinja docs and noted the following options on the Jinja Environment object:

block_start_string The string marking the beginning of a block. Defaults to '{%'. block_end_string The string marking the end of a block. Defaults to '%}'. variable_start_string The string marking the beginning of a print statement. Defaults to '{{'. variable_end_string The string marking the end of a print statement. Defaults to '}}'.

What are your thoughts on changing those to something else, eg square brackets such as [[ .. ]] and [% ... %] or angle brackets such as <{ ... }> and <% ... %>? (Other ideas?)

Given that such a change would be a breaking one, I was thinking that it could be added as an option in the lovelace_gen: configuration.yaml entry. For easier entry for new users going forward, I feel like this change should be the default, but that would require action on the part of existing users...

I'm more than happy to throw in a PR for this, but wanted to get your thoughts beforehand.

thomasloven commented 3 years ago

Lovelace_gen doesn't provide the jinja engine by itself, but instead piggy-backs on the one used by Home Assistant, including all settings for it. I also feel reluctant about adding to or changing a syntax that most hass users are already aware of and (to a varying extent) comfortable with.

Matchlighter commented 3 years ago

The former isn't an issue since you're already creating a separate Jinja Environment strictly for Lovelace Gen, (jinja = jinja2.Environment(loader=jinja2.FileSystemLoader("/"))) so any custom symbols would be limited only to Lovelace Gen. What do you think about making this feature optional via a config parameter?

lmamakos commented 3 years ago

I was thinking about this same sort of thing myself. I might be clever to have the #lovelace_gen first line in the file carry options to specify the various quoting characters. I think that you can pass in replacements for {{ }}, {% %} and {# #} when the template engine is instantiated.