Does the Jinja2 templating language have the concept of 'here' (current directory)?

后端 未结 4 1888
花落未央
花落未央 2020-12-05 06:54

Does Jinja2 support template-relative paths e.g. %(here)s/other/template.html, to include other templates relative to the current template\'s place in the files

4条回答
  •  囚心锁ツ
    2020-12-05 07:52

    Just to add to Will McCutchen's answer,

    You can have multiple directories in your loader. It then searches in each of the directories (in order) until it finds the template.

    for example, if you wanted to have "sidebar.html" instead of "/includes/sidebar.html" then have:

    loader=jinja2.FileSystemLoader(
            [os.path.join(os.path.dirname(__file__),"templates/includes"),
             os.path.join(os.path.dirname(__file__),"templates")])
    

    instead of

    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__),"templates"))
    

提交回复
热议问题