'How to create multiple sub-folders in cookiecutter template using a sub-folder template
I want to create a template for our internal projects.
The layout of the project contains a variable amount of sub-folders.
I would like to specify the sub-folders to create, inside some configuration file. For example, I have the following folder structure:
my_project
|
|___ Tables
| |
| |__ Table1 <--- The folders I would like to create
| |__ Table2 <---|
| |__ Table3 <---|
|
|___ Other_folders
The configuration file should have something like this:
{
"prj_name": "my_project",
"tables": ["Table1", "Table2", "Table3"]
}
I have tried to create the following structure:
{{cookiecutter.prj_name}}
|
|___ Tables
| |
| |__ {% for table in cookiecutter.tables %}{{table}}{% endfor %}
|
|___ Other_folders
and added to cookiecutter.json the mentioned configuration.
I ran cookiecutter using --no-input. And the result was only a single sub-folder (Table1).
Also tried to use the following folder name: {% for table in cookiecutter.tables.keys() %}{{table}}{% endfor %}
With the following configuration:
{
"prj_name": "my_project",
"tables": {
"table1": "table1",
"table2": "table2",
"table3": "table3"
}
}
The output was still a single sub-folder (Table2Table1Table3)
Any idea how I can achieve the desired structure?
Solution 1:[1]
Well eventually I wrote a python script that uses Cookiecutter's package functions with two template folders.
One template folder for the main project, and one template folder for the tables folders.
And the script would use cookiecutter.main.cookiecutter function to create the main project, and the sub folders in a loop.
I've wrote a more detailed post in a blog. Not sure about the rules here about posting links to personal blogs. So if someone in the future would like to see it, let me know in the comments.
Solution 2:[2]
I went with a low-tech solution here. My use case was pretty simple and there's a finite amount of folders I needed to create so my solution is manageable to me. YMMV, but I thought I'd offer it here in case it works for someone else.
I basically prepopulated the number of directories I figure I would ever need. I just used Python's format in a range loop to prepopulate however many folders I needed.
cookiecutter/mycookiecutter/
??? cookiecutter.json
??? {{cookiecutter.project_name}}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 0\ %}{{cookiecutter.account_info["accounts"][0]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 1\ %}{{cookiecutter.account_info["accounts"][1]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 10\ %}{{cookiecutter.account_info["accounts"][10]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 11\ %}{{cookiecutter.account_info["accounts"][11]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 12\ %}{{cookiecutter.account_info["accounts"][12]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 13\ %}{{cookiecutter.account_info["accounts"][13]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 14\ %}{{cookiecutter.account_info["accounts"][14]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 15\ %}{{cookiecutter.account_info["accounts"][15]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 16\ %}{{cookiecutter.account_info["accounts"][16]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 17\ %}{{cookiecutter.account_info["accounts"][17]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 18\ %}{{cookiecutter.account_info["accounts"][18]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 19\ %}{{cookiecutter.account_info["accounts"][19]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 2\ %}{{cookiecutter.account_info["accounts"][2]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 3\ %}{{cookiecutter.account_info["accounts"][3]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 4\ %}{{cookiecutter.account_info["accounts"][4]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 5\ %}{{cookiecutter.account_info["accounts"][5]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 6\ %}{{cookiecutter.account_info["accounts"][6]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 7\ %}{{cookiecutter.account_info["accounts"][7]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 8\ %}{{cookiecutter.account_info["accounts"][8]}}{%\ endif\ %}
??? {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 9\ %}{{cookiecutter.account_info["accounts"][9]}}{%\ endif\ %}
cookiecutter.json
{
"project_name": "test",
"account_info": {
"accounts": [
"root",
"corp",
"dns",
"dev",
"prod"
]
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Igor Basko |
| Solution 2 | srhopkins |
