I am creating a cookiecutter template and would like to add a folder (and the files it contains) only if a variable has a given value. For example cookiecutter.json:
{
"project_slug":"project_folder"
"i_want_this_folder":['y','n']
}
and my template structure looks like:
template
└── {{ cookiecutter.project_slug }}
├── config.ini
├── data
│ └── data.csv
├── {% if cookiecutter.i_want_this_folder == 'y' %}my_folder{% endif %}
└── some_files
However, when running cookiecutter template
and choose 'n' I get an error
Error: "~/project_folder" directory already exists
Is my syntax for the folder name correct?
I was facing the same issue having the option to add or no folders with different contents (all folders can exist at the same time). The structure of the project is the following:
where the
cookiecutter.json
contains the followingas each directory
folder_X_to_add_or_no
contains different files, the trick is to remove those folders that the answer is "no", you can do this through a hook. Inside thepost_gen_project.py
fileNow the folders the user choose not to add will be removed.
References
This answer is based on briancapello answer on this github issue