blueprint-resources#Templates部分翻译

发表于:2018-06-20 23:35:27
更新于:2018-06-20 23:35:27

相关issue:
Method render_template does not use blueprint specified template_folder #1361
官方文档:
blueprint-resources#Templates

Templates

模板

If you want the blueprint to expose templates you can do that by providing the template_folder parameter to the :class:Blueprint constructor::
如果你想让蓝图暴露一些模板(比如flask-bootstrap扩展提供的"bootstrap/base.html"),
可以给在构造Blueprint时,指定template_folder参数。

           admin = Blueprint('admin', __name__, template_folder='templates')

For static files, the path can be absolute or relative to the blueprint resource folder. 对于静态的文件,路径可以是绝对或相对于蓝图资源文件夹的。

The template folder is added to the search path of templates but with a lower priority than the actual application's template folder. That way you can easily override templates that a blueprint provides in the actual application. This also means that if you don't want a blueprint template to be accidentally overridden, make sure that no other blueprint or actual application template has the same relative path. When multiple blueprints provide the same relative template path the first blueprint registered takes precedence over the others.
模板的全局搜索路径中会自动添加这个模板文件夹,并且它的优先级低于应用的模板文件夹。这样, 你可以在实际应用中,轻而易举地覆盖某个蓝图的模板。也就是说,如果不想让某个蓝图模板 莫名其妙地被覆盖,请确保其他的蓝图的模板相对路径不同于实际应用的模板相对路径。假如 多个蓝图模板被指定了相同的相对路径,那么,当flask搜索模板的时候,先被注册的蓝图模板 优先于后被注册的蓝图模板。

So if you have a blueprint in the folder yourapplication/admin and you want to render the template 'admin/index.html' and you have provided templates as a template_folder you will have to create a file like this: :file:yourapplication/admin/templates/admin/index.html. The reason for the extra admin folder is to avoid getting our template overridden by a template named index.html in the actual application template folder.
所以说,如果你在yourapplication/admin文件夹中编写了一个admin蓝图,而且你还想 渲染admin/index.html,并且你也给 template_folder参数赋值了一个templates, 那你就得像这样创建你的文件yourapplication/admin/templates/admin/index.html, 额外的那个admin文件夹的存在,是为了不让实际应用的模板文件夹,把我们admin 蓝图的index.html文件覆盖掉。

To further reiterate this: if you have a blueprint named admin and you want to render a template called :file:index.html which is specific to this blueprint, the best idea is to lay out your templates like this::
再说一遍,如果你有一个名为admin的蓝图,并且你想渲染这个蓝图中的一个名为 index.html的模板,最好的办法,就是把admin蓝图的模板路径弄成像这样:

    yourpackage/
        blueprints/
            admin/
                templates/
                    admin/
                        index.html
                __init__.py

And then when you want to render the template, use :file:admin/index.html as the name to look up the template by. If you encounter problems loading the correct templates enable the EXPLAIN_TEMPLATE_LOADING config variable which will instruct Flask to print out the steps it goes through to locate templates on every render_template call.
此后,每当你想渲染admin蓝图的模板,只需在调用render_template函数时,把参数写成 admin/index.html就行了。假如你加载模板时遇到了问题,你可以把flask提供的一个名为 EXPLAIN_TEMPLATE_LOADING的配置项设置为可用。一旦启用了该配置,Flask就会在调用 render_template函数渲染模板时,把搜索模板的步骤全部输出出来,非常方便。