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

后端 未结 4 1574
花落未央
花落未央 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: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.

    0 讨论(0)
  • 2020-12-05 07:42

    According to the documentation for jinja2.Environment.join_path(), support for relative template paths is possible by overriding join_path() to implement "template path joining".

    class RelEnvironment(jinja2.Environment):
        """Override join_path() to enable relative template paths."""
        def join_path(self, template, parent):
            return os.path.join(os.path.dirname(parent), template)
    
    0 讨论(0)
  • 2020-12-05 07:42

    The cleanest way to overcome this limitation, would be with a jinja2 extension that will allow to import relative template names

    Something in the likes of:

    from jinja2.ext import Extension
    import re
    
    
    class RelativeInclude(Extension):
        """Allows to import relative template names"""
        tags = set(['include2'])
    
        def __init__(self, environment):
            super(RelativeInclude, self).__init__(environment)
            self.matcher = re.compile("\.*")
    
        def parse(self, parser):
            node = parser.parse_include()
            template = node.template.as_const()
            if template.startswith("."):
                # determine the number of go ups
                up = len(self.matcher.match(template).group())
                # split the current template name into path elements
                # take elements minus the number of go ups
                seq = parser.name.split("/")[:-up]
                # extend elements with the relative path elements
                seq.extend(template.split("/")[1:])
                template = "/".join(seq)
                node.template.value = template
            return node
    
    0 讨论(0)
  • 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"))
    
    0 讨论(0)
提交回复
热议问题