我想为我们的内部项目创建一个模板。
项目的布局包含数量可变的子文件夹。
我想在一些配置文件中指定要创建的子文件夹。 例如,我有以下文件夹结构:
my_project
|
|___ Tables
| |
| |__ Table1 <--- The folders I would like to create
| |__ Table2 <---|
| |__ Table3 <---|
|
|___ Other_folders
配置文件应该是这样的:
{
"prj_name": "my_project",
"tables": ["Table1", "Table2", "Table3"]
}
我尝试创建以下结构:
{{cookiecutter.prj_name}}
|
|___ Tables
| |
| |__ {% for table in cookiecutter.tables %}{{table}}{% endfor %}
|
|___ Other_folders
并将提到的配置添加到 cookiecutter.json
。
我使用 --no-input
运行 cookiecutter。结果只有一个子文件夹 (Table1)。
还尝试使用以下文件夹名称:{% for table in cookiecutter.tables.keys() %}{{table}}{% endfor %}
使用以下配置:
{
"prj_name": "my_project",
"tables": {
"table1": "table1",
"table2": "table2",
"table3": "table3"
}
}
输出仍然是单个子文件夹 (Table2Table1Table3)
知道如何实现所需的结构吗?
最佳答案
好吧,最终我写了一个使用 Cookiecutter's package functions 的 python 脚本有两个模板文件夹。
一个模板文件夹用于主项目,一个模板文件夹用于表文件夹。
脚本会使用 cookiecutter.main.cookiecutter
函数创建主项目,并循环创建子文件夹。
我在博客中写了一篇更详细的文章。 不确定这里关于发布个人博客链接的规则。因此,如果以后有人想看它,请在评论中告诉我。
关于python - 如何使用子文件夹模板在 cookiecutter 模板中创建多个子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60025115/