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

Not able to template piped variables #67

Open
hesoyamhh opened this issue Mar 24, 2021 · 1 comment
Open

Not able to template piped variables #67

hesoyamhh opened this issue Mar 24, 2021 · 1 comment

Comments

@hesoyamhh
Copy link

My dummy test.j2 file contains: Hi {{ data_var | env_var | d('default_var') }}
My data_test.yaml contains: data_var: foo_yaml
I have set env_var via: export env_var='foo_env'

So, as per templating:

Case-1: j2 --undefined test.j2 data_test.yaml -o test should output Hi foo_yaml

Case-2: j2 --undefined test.j2 -o test should output Hi foo_env

Case-3: if I execute unset env_var and then execute j2 --undefined test.j2 -o test, it should output Hi default_var

But in all cases, it throws error:

Traceback (most recent call last):
  File "/usr/local/bin/j2", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/site-packages/j2cli/cli.py", line 206, in main
    sys.argv[1:]
  File "/usr/local/lib/python2.7/site-packages/j2cli/cli.py", line 186, in render_command
    result = renderer.render(args.template, context)
  File "/usr/local/lib/python2.7/site-packages/j2cli/cli.py", line 86, in render
    .get_template(template_path) \
  File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/usr/local/lib/python2.7/site-packages/jinja2/loaders.py", line 125, in load
    code = environment.compile(source, name, filename)
  File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 591, in compile
    self.handle_exception(exc_info, source_hint=source_hint)
  File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/tmp/test.j2", line 1, in template
    Hi {{ data_var | env_var | d('default_var') }}
jinja2.exceptions.TemplateAssertionError: no filter named 'env_var'

So, not only piped templating doesn't happen, it also ignores --undefined

@hesoyamhh hesoyamhh reopened this Mar 25, 2021
@ameyagokhale
Copy link

You need to modify your test.j2 to Hi {{ data_var | d(env_var) | d('default_var') }}

bash-5.0$ cat test.j2
Hi {{ data_var | d(env_var) | d('default_var') }}
bash-5.0$
bash-5.0$ cat data_test.yaml
data_var: foo_yaml

bash-5.0$
bash-5.0$ export env_var='foo_env_var'
bash-5.0$
bash-5.0$ echo $env_var
foo_env_var
bash-5.0$
bash-5.0$
bash-5.0$ j2 --undefined -o test test.j2 && cat test
Hi foo_env_var
bash-5.0$
bash-5.0$ j2 --undefined -o test test.j2 data_test.yaml && cat test
Hi foo_yaml
bash-5.0$
bash-5.0$ unset env_var
bash-5.0$
bash-5.0$ j2 --undefined -o test test.j2 && cat test
Hi default_var
bash-5.0$

It will work as expected

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants