Skip to content

Commit 14fd723

Browse files
committed
Add fromjson filter
1 parent 38fd8b7 commit 14fd723

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ name: {{ name }}
150150

151151
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.
152152

153+
> Note: If you want to pass a dictionary of values into a file, you need to convert it to json first:
154+
> ```yaml
155+
> {% set mydict = {"a": 1, "b": 2} %}
156+
> variable: {{ mydict | tojson }}
157+
> ```
158+
> And then convert it back from json inside the file:
159+
> ```yaml
160+
> content: The value of a is {{ (variable | fromjson)['a'] }}
161+
> ```
162+
>
163+
> The `fromjson` filter is a feature of `lovelace_gen` and not normally included in jinja.
164+
153165
## Invalidate cache of files
154166

155167
If you use lots of custom lovelace cards, chances are that you have run into caching problems at one point or another.

custom_components/lovelace_gen/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212

1313
_LOGGER = logging.getLogger(__name__)
1414

15+
def fromjson(value):
16+
return json.loads(value)
17+
1518
jinja = jinja2.Environment(loader=jinja2.FileSystemLoader("/"))
1619

20+
jinja.filters['fromjson'] = fromjson
21+
1722
llgen_config = {}
1823

1924
def load_yaml(fname, args={}):
@@ -89,4 +94,4 @@ def compose_node(self, parent, index):
8994
self.ascend_resolver()
9095
return node
9196

92-
yaml.composer.Composer.compose_node = compose_node
97+
yaml.composer.Composer.compose_node = compose_node

0 commit comments

Comments
 (0)