从父文件夹获取部分模板

时间:2019-02-18 16:23:51

标签: jinja2

我的项目分为多个模块。

每个模块都有自己的/templates文件夹,其中包含自己的相关模板文件。

我希望将这些模板文件中的通用组件(标头,导航)提取为“部分”,然后将它们包含在每个模块的模板文件中(例如:{% include 'nav.html' %}

问题

如何设置模板环境,以允许模块的模板文件进入包含这些部分的shared文件夹中?

我当前的环境设置

template_dir = os.path.join(os.path.dirname(__file__), 'views')
env = jinja2.Environment(
    loader=jinja2.FileSystemLoader(template_dir), 
    autoescape=True)

self.render('some_template.html', env=env)   # render function

此当前配置仅允许我进入每个模块内的/templates文件夹。但是,模板文件(在/templates目录中)应该 也可以访问每个模块目录之外的部分文件。

理想情况下,应用程序目录如下所示:

app/
  shared/
    header.html <-- templates within modules need to access these
    nav.html
  modules/
    module_a/
      handler.py
      templates/
        my_template.html  <-- needs access to partials from 'shared/' 
    module_b/
    module_c/

0 个答案:

没有答案
相关问题