Skip to content

Commit

Permalink
adds conditional support to nested template objects
Browse files Browse the repository at this point in the history
This change allows conditionals to be applied to sub keys when
templating json data structures. The conditional can be applied using
the `when:` directive
  • Loading branch information
Peter Sprygada authored and trishnaguha committed May 17, 2018
1 parent ddf35e6 commit a624c4b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/network_engine/plugins/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ def _update(self, d, u):
else:
d[k] = v
return d

def _check_conditional(self, when, variables):
conditional = "{%% if %s %%}True{%% else %%}False{%% endif %%}"
return self.template(conditional % when, variables)
8 changes: 4 additions & 4 deletions lib/network_engine/plugins/template/json_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def run(self, template, variables=None):
key = self.template(item['key'], variables)

# FIXME moving to the plugin system breaks this
# when = item.get('when')
# if when is not None:
# if not self._check_conditional(when, variables):
# continue
when = item.get('when')
if when is not None:
if not self._check_conditional(when, variables):
continue

if 'value' in item:
value = item.get('value')
Expand Down

0 comments on commit a624c4b

Please sign in to comment.