New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature-Request: Templates in context variables should support boolean values #2025
Comments
There's no simple functionality in Jinja2 to base this on.
Cookiecutter would need to make two rendering passes to support this. |
What would you think of adding a metadata variable to cookiecutter.json (similar to {
"_variable_types": {
"likes_mashed_potatoes": "Boolean"
},
"likes_potatoes": false,
"likes_mashed_potatoes": "{{ cookiecutter.likes_potatoes }}"
} |
I didn't quite notice at first, that currently Cookiecutter already makes multiple rendering passes over the values in cookiecutter.json that are templates, to pick up self references. But by definition, a text templating language like Jinja2 can only render strings in those passes, not booleans. So cookiecuter would have to read the metadata and remove it from the rendering context itself If it supported cookiecutter.yml, wouldn't you be able to achieve the same thing with Yaml Anchors, Aliases, or even Tags, with much less new code required in Cookiecutter, without altering the way thw code treats existing templates that use cookiecutter.json? |
You're suggesting something like the following if YAML was supported? likes_potatoes: false
likes_mashed_potatoes: !!bool "{{ cookiecutter.likes_potatoes }}" If so, I think that would work for me. I just need cookiecutter to know that |
I'm suggesting Yaml anchors, and that cookiecutter just lets a good Yaml library handle everything:
|
The problem with YAML anchors is that I need the second value to be a template, so I can accomplish something like: potato_preference: ['mashed', 'boiled', 'roasted', 'raw']
should_boil_water: "{{ cookiecutter.potato_preference == 'boiled' }}" I'd like I don't see a way to use YAML anchors to accomplish that, unless I'm missing something. |
Fair enough. It's still a huge change to start processing different data types, within what's essentially a sophisticated text/string global search/replace program, that delegates much of the text/string handling to Jinja2. Does it just need to be a Boolean, only for that test within Jinja? Or does something else also use your cookiecutter.json? Other templates avoid the need for Booleans by simply writing a more explicit test, e.g. using 'y', or anything else.
|
This was the solution we ended up going with for now, comparing the variable's value with "yes"/"no" strings. Just nice to use Booleans where we can (also nice to get the |
This issue is a reopening of #1738.
Description:
As described here, we expect template authors to be able to reuse values of previous keys from cookiecutter.json, but currently that is not possible if you are working with boolean values. I believe it would be very important to support this use case or describe an alternative way to do it in docs. Example:
cookiecutter.json:
Template example using above variables:
The
echo "I like mashed potatoes"
line always renders, even whenlikes_potatoes
is false.The text was updated successfully, but these errors were encountered: