1
我在路径中有几个jinja模板文件,我想渲染它们并将它们写入文件,我的问题是它们的输出文件名,如何从Jinja模板调用方法?
是否有可能在jinja模板中定义一个函数来返回并准备模板输出文件名,并从python代码中调用它并检索它的值?
这是我的代码:
#in here inputPath is jinja templates path
for root, dirs, files in os.walk(inputPath):
for file in files:
fPath = root[len(inputPath) + 1:]
newPath = (fPath + "/" if fPath else '') + file
template = env.get_template(newPath)
oPath = os.path.join(outputPath, fPath)
try:
os.makedirs(oPath)
except:
pass
oPath=os.path.join(oPath,file)
with codecs.open(oPath,'w', "utf-8") as f:
f.write(template.render(projectname=projectName, mainxmlpath=mainXamlPath))
在这段代码的输出文件名
正是神社模板文件名。