Cookiecutter: Allow boolean config variables

Created on 27 Nov 2013  路  5Comments  路  Source: cookiecutter/cookiecutter

cookiecutter.json allows a key-value configuration, where value is a string. I would like to ask for a boolean (yes/no) variable to condition a block like this

    {% if use_that_magic %}
         ....
    {% endif %}

The easiest way should be autodetect the variable type from the default value. For example

{ "full_name": "Mart铆n Gait谩n",
  "use_that_magic": true,  
   ...
}

When prompt for use_that_magic the valid answer should be yes or no.

Also, could be an attribute type when implement #30

enhancement

Most helpful comment

Another reasonable option, perhaps cleaner, is to use 0 and 1 with Jinja's int filter. For example...

cookiecutter.json:

{
  "my_bool": 0,
}

or

{
  "my_bool": 1,
}

A file in your cookiecutter template:

{% if cookiecutter.my_bool|int %}
  my_bool is truthy
{% else %}
  my_bool is falsy
{% endif %}

All 5 comments

+1, I'd like this

I was able to hack around it by specifying a default of "" in the config, so that any value counts as True, and the default is False, but that isn't really intuitive to other users of the template.

+1 boolean support would be nice.

This would be possible with #848

I'm currently working around lack of boolean support by using a choice variable with strings (["yes", "no"]) and == string comparisons in Jinja. This might help others who wind up here looking for bool support.

Another reasonable option, perhaps cleaner, is to use 0 and 1 with Jinja's int filter. For example...

cookiecutter.json:

{
  "my_bool": 0,
}

or

{
  "my_bool": 1,
}

A file in your cookiecutter template:

{% if cookiecutter.my_bool|int %}
  my_bool is truthy
{% else %}
  my_bool is falsy
{% endif %}
Was this page helpful?
0 / 5 - 0 ratings