Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 324
Igor Basko Avatar asked Nov 24 '25 04:11

Igor Basko


1 Answers

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.

like image 58
Igor Basko Avatar answered Nov 25 '25 19:11

Igor Basko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!