Add fromjson filter

This commit is contained in:
Thomas Lovén 2020-01-16 12:30:51 +01:00
parent 38fd8b779f
commit 14fd723176
2 changed files with 18 additions and 1 deletions

View File

@ -150,6 +150,18 @@ name: {{ name }}
Be careful about the syntax here. Note that the arguments are given as a list and is indented under the `!include` statement. The second item in the list is a dictionary. Be careful about the syntax here. Note that the arguments are given as a list and is indented under the `!include` statement. The second item in the list is a dictionary.
> Note: If you want to pass a dictionary of values into a file, you need to convert it to json first:
> ```yaml
> {% set mydict = {"a": 1, "b": 2} %}
> variable: {{ mydict | tojson }}
> ```
> And then convert it back from json inside the file:
> ```yaml
> content: The value of a is {{ (variable | fromjson)['a'] }}
> ```
>
> The `fromjson` filter is a feature of `lovelace_gen` and not normally included in jinja.
## Invalidate cache of files ## Invalidate cache of files
If you use lots of custom lovelace cards, chances are that you have run into caching problems at one point or another. If you use lots of custom lovelace cards, chances are that you have run into caching problems at one point or another.

View File

@ -12,8 +12,13 @@ from homeassistant.exceptions import HomeAssistantError
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
def fromjson(value):
return json.loads(value)
jinja = jinja2.Environment(loader=jinja2.FileSystemLoader("/")) jinja = jinja2.Environment(loader=jinja2.FileSystemLoader("/"))
jinja.filters['fromjson'] = fromjson
llgen_config = {} llgen_config = {}
def load_yaml(fname, args={}): def load_yaml(fname, args={}):