2016-06-21 56 views

回答

7

你可以在Jinja本身中做到這一點,你只需要編寫一個腳本來讀取和解析模板。

既然你關心以及形成模板,而不是變量是否是可用的,它應該是很容易做到:

# filename: check_my_jinja.py 
import sys 
from jinja2 import Environment 

env = Environment() 
with open(sys.argv[1]) as template: 
    env.parse(template.read()) 

或東西,遍歷所有模板

# filename: check_my_jinja_recursive.py 
import sys 
import os 
from jinja2 import Environment 

env = Environment() 
templates = [d + f for d, _, f in os.walk(mytemplates) if f.endswith('.jinja2')] 
for template in templates: 
    with open(template) as t: 
     env.parse(t.read()) 

如果你有不正確的語法,你會得到一個TemplateSyntaxError

所以你precommi t勾可能看起來像

python check_my_jinja.py template.jinja2 
python check_my_jinja_recursive.py /dir/templates_folder 
相關問題