Skip to content

Commit

Permalink
Add fromjson filter
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Jan 16, 2020
1 parent 38fd8b7 commit 14fd723
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

> 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

If you use lots of custom lovelace cards, chances are that you have run into caching problems at one point or another.
Expand Down
7 changes: 6 additions & 1 deletion custom_components/lovelace_gen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@

_LOGGER = logging.getLogger(__name__)

def fromjson(value):
return json.loads(value)

jinja = jinja2.Environment(loader=jinja2.FileSystemLoader("/"))

jinja.filters['fromjson'] = fromjson

llgen_config = {}

def load_yaml(fname, args={}):
Expand Down Expand Up @@ -89,4 +94,4 @@ def compose_node(self, parent, index):
self.ascend_resolver()
return node

yaml.composer.Composer.compose_node = compose_node
yaml.composer.Composer.compose_node = compose_node

0 comments on commit 14fd723

Please sign in to comment.