I have a few jinja template file in a path, I want to render them and write them in a file, my problem is their output file name,
Is it possible that define a function in jinja template to return and prepare the template output file name, and call it from python code and retrieve its value?
this is my code:
#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))
in this code output file name is exactly jinja template file name.
I've solved it,
Template class has module attribute that contains all your template methods and defined variables
for example template.module.foo()
calls your foo
macro in the jinja template.