Closed
Description
Describe the problem
I use jinja import like this:
{% from 'common.jinja' import var1 %}
{{var1}}
it works inside a static file with a name like file.jinja
. However when I try to use the above import in a conditional file named {% if shouldCreate %}file{% endif %}.jinja
it gives an error as in the logs section.
Template
copier.yaml:
shouldCreate:
type: bool
_subdirectory: template
common.jinja:
{% set var1 = "test" %}
template/{% if shouldCreate %}file{% endif %}.jinja
{% from 'common.jinja' import var1 %}
{{var1}}
To Reproduce
No response
Logs
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/bin/copier", line 8, in <module>
sys.exit(copier_app_run())
^^^^^^^^^^^^^^^^
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/plumbum/cli/application.py", line 639, in run
inst, retcode = subapp.run(argv, exit=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/plumbum/cli/application.py", line 634, in run
retcode = inst.main(*tailargs)
^^^^^^^^^^^^^^^^^^^^
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/copier/cli.py", line 68, in _wrapper
return method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/copier/cli.py", line 295, in main
).run_copy()
^^^^^^^^^^
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/copier/main.py", line 715, in run_copy
self._render_folder(src_abspath)
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/copier/main.py", line 607, in _render_folder
self._render_file(file)
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/copier/main.py", line 524, in _render_file
tpl = self.jinja_env.get_template(src_relpath)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/jinja2/environment.py", line 1010, in get_template
return self._load_template(name, globals)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/jinja2/environment.py", line 969, in _load_template
template = self.loader.load(self, name, self.make_globals(globals))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/jinja2/loaders.py", line 138, in load
code = environment.compile(source, name, filename)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/jinja2/environment.py", line 766, in compile
return self._compile(source, filename)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jsmith/.local/pipx/.cache/93c3d6722ebba87/lib/python3.11/site-packages/jinja2/environment.py", line 704, in _compile
return compile(source, filename, "exec") # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/jsmith/Desktop/temp/my-service/template/{% if shouldCreate %}file{% endif %}.jinja", line 15
(% if shouldCreate %)
^
SyntaxError: f-string: invalid syntax
Expected behavior
Import should work in conditional files.
Screenshots/screencasts/logs
No response
Operating system
macOS
Operating system distribution and version
13.3.1
Copier version
copier 7.2.0
Python version
Python 3.10.4
Installation method
pipx+pypi
Additional context
No response