sambhav / cookiecutter-advanced-demo

Cookiecutter demo-ing how to modify cookiecutter context from hooks
21 stars 2 forks source link

How do I update/add a cookiecutter content value with a variable in pre_gen_project.py? #1

Closed ws1088 closed 3 years ago

ws1088 commented 3 years ago

The following doesn't seems to work in pre_gen_project.py

my_var = "abc"
{{ cookiecutter.update({"module_name": my_var }) }} 

Is it possible?

sambhav commented 3 years ago

{% set testing = 'it worked' %}

You can find everything in Jinja2 docs :) https://jinja.palletsprojects.com/en/2.11.x/

ljohnston commented 3 years ago

Wondering if you could elaborate here just a little bit? I'm new to this jinja2 stuff and it's still something of a mystery. I read through some of the docs and tried a couple of things based on your comment above, but couldn't get anything to work.

In pre_gen_project.py:

{% set testing = 'it worked' %}
{{ cookiecutter.update({"test": "{{ testing }}"}) }}

Then, in a file that's being rendered:

{{ cookiecutter.test }}

But the rendered file contains:

"{{ testing }}"

I get a syntax error if I instead try and do the update() call like this:

{{ cookiecutter.update({"test": {{ testing }}}) }}

The error:

jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'

Thanks, Lance