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

后端 未结 4 1878
花落未央
花落未央 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条回答
  •  猫巷女王i
    2020-12-05 07:37

    I do not believe so. Typically you include or extend other templates by specifying their paths relative to the root of whatever template loader and environment you're using.

    So let's say your templates are all in /path/to/templates and you've set up Jinja like so:

    import jinja2
    template_dir = '/path/to/templates'
    loader = jinja2.FileSystemLoader(template_dir)
    environment = jinja2.Environment(loader=loader)
    

    Now, if you'd like to include /path/to/templates/includes/sidebar.html in the /path/to/templates/index.html template, you'd write the following in your index.html:

    {% include 'includes/sidebar.html' %}
    

    and Jinja would figure out how to find it.

提交回复
热议问题