Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
fix: Make render available as global function
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Mar 4, 2024
1 parent bc1b3c5 commit f7191e3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion jinja2_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ func TestGetVarAndRender(t *testing.T) {
{tmpl: "{% set loc = 'l1' %}{{ get_var('loc') }}", result: "l1"},
// TODO test for this when https://github.com/pallets/jinja/issues/1478 gets fixed
// {tmpl: "{% for e in list %}{{ get_var('e.x') }}{% endfor %}", result: ""},
// same is before, but with a workaround to make 'e' a local variable
{tmpl: "{% for e in list %}{% set e=e %}{{ get_var('e.x') }}{% endfor %}", result: "i1i2"},
{tmpl: "{% set loc = 'l1' %}{{ '{{ loc }}' | render }}", result: "l1"},
// TODO test for this when https://github.com/pallets/jinja/issues/1478 gets fixed
// {tmpl: "{% for e in list %}{{ '{{ e }}' | render }}{% endfor %}", result: ""},
// {tmpl: "{% for e in list %}{{ render('{{ e }}') }}{% endfor %}", result: ""},
// same is before, but with a workaround to make 'e' a local variable
{tmpl: "{% for e in list %}{% set e=e %}{{ render('{{ e.x }}') }}{% endfor %}", result: "i1i2"},
}

for i, tc := range tests {
Expand Down
4 changes: 4 additions & 0 deletions python_src/go_jinja2/ext/kluctl_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def from_yaml(s):

@jinja2.pass_context
def render(ctx, string):
# TODO we should deprecate render being available as filter, as filters seem to not have access to local variables
t = ctx.environment.from_string(string)
return t.render(ctx.get_all())

Expand Down Expand Up @@ -116,6 +117,8 @@ def add_jinja2_filters(jinja2_env):
jinja2_env.filters['to_yaml'] = to_yaml
jinja2_env.filters['to_json'] = to_json
jinja2_env.filters['from_yaml'] = from_yaml
# render is available as filter and as global function. The filter should be deprecated in the future as it is
# unable to access local variables
jinja2_env.filters['render'] = render
jinja2_env.filters['sha256'] = sha256
jinja2_env.filters['slugify'] = slugify
Expand All @@ -126,3 +129,4 @@ def add_jinja2_filters(jinja2_env):
jinja2_env.globals['raise'] = raise_helper
jinja2_env.globals['debug_print'] = debug_print
jinja2_env.globals['load_sha256'] = load_sha256
jinja2_env.globals['render'] = render

0 comments on commit f7191e3

Please sign in to comment.