Jinja2模板语言是否具有“这里"(当前目录)的概念? [英] Does the Jinja2 templating language have the concept of 'here' (current directory)?

查看:57
本文介绍了Jinja2模板语言是否具有“这里"(当前目录)的概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jinja2是否支持模板相对路径,例如%(here)s/other/template.html,要包括相对于当前模板在文件系统中位置的其他模板?

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 filesystem?

推荐答案

我不这么认为.通常,您可以通过指定相对于您正在使用的任何模板加载器和环境的根目录的路径来包括或扩展其他模板.

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.

因此,假设您的模板全部位于/path/to/templates中,并且您已经按照以下方式设置了Jinja:

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)

现在,如果要在/path/to/templates/index.html模板中包含/path/to/templates/includes/sidebar.html,则可以在index.html中编写以下内容:

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' %}

Jinja会找出如何找到它的.

and Jinja would figure out how to find it.

这篇关于Jinja2模板语言是否具有“这里"(当前目录)的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆