[英]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.并添加到
cookiecutter.json
提到的配置。
I ran cookiecutter using --no-input
.我使用
--no-input
运行 cookiecutter。 And the result was only a single sub-folder (Table1).结果只有一个子文件夹(表1)。
Also tried to use the following folder name: {% for table in cookiecutter.tables.keys() %}{{table}}{% endfor %}
With the following configuration:还尝试使用以下文件夹名称:
{% for table in cookiecutter.tables.keys() %}{{table}}{% endfor %}
使用以下配置:
{
"prj_name": "my_project",
"tables": {
"table1": "table1",
"table2": "table2",
"table3": "table3"
}
}
The output was still a single sub-folder (Table2Table1Table3)输出仍然是单个子文件夹 (Table2Table1Table3)
Any idea how I can achieve the desired structure?知道如何实现所需的结构吗?
Well eventually I wrote a python script that uses Cookiecutter's package functions with two template folders.好吧,最终我写了一个 python 脚本,它使用Cookiecutter 的包函数和两个模板文件夹。
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.并且脚本将使用
cookiecutter.main.cookiecutter
函数来创建主项目,并在循环中创建子文件夹。
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.
因此,如果将来有人想看它,请在评论中告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.