How to use dynamic variables in cookiecutter hooks?

1.1k Views Asked by At

I would like to use dynamic variables inside hooks/pre_gen_project.py.

When I add a static text inside the hook file it works:

{{ cookiecutter.update({"venv_path": "static/venvpath", "py_path": "static/pypath"}) }}

But when I tried to use a variable like venv_path_val

venv_path_val = "static/venvpath"
py_path_val = "static/pypath"
{{ cookiecutter.update({"venv_path": venv_path_val, "py_path": py_path_val }) }}

I am getting

jinja2.exceptions.UndefinedError: 'venv_path_val' is undefined

I also tried it inside eval but no luck:

eval("{{ '{{'|safe }} cookiecutter.update({'venv_path': venv_path, 'py_path': py_path}) {{ '}}'|safe }}")

So is there a way to inject my dynamic template variables inside cookiecutter?

1

There are 1 best solutions below

0
On

I haven't figured this out either. An alternative to this would be to inject extra context

In your case, add the following to your hooks/pre_gen_project.py

from cookiecutter.main import cookie-cutter

venv_path = "static/venvpath"
py_path = "static/pypath"

cookiecutter(
    'cookiecutter-django',
    extra_context={
        'venv_path': venv_path,
        'py_path': py_path
    })
)