2

パスにいくつかの 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))

このコードの出力ファイル名は、正確に jinja テンプレート ファイル名です。

4

1 に答える 1

2

解決しました、

テンプレート クラスには、すべてのテンプレート メソッドと定義済み変数を含む module 属性があります。

たとえば、jinja テンプレートでマクロをtemplate.module.foo()呼び出します。foo

于 2012-07-22T12:21:29.917 に答える