如何从Flask蓝图的多个template_folder加载? [英] How to load from more then one template_folder for Flask blueprint?
问题描述
#blueprint code $ b $ from flask import Blueprint
from pkg_resources import resource_filename
app = Blueprint('formgear',__name__,
template_folder = resource_filename('formgear','templates'))
pre> 现在我想添加另一组模板,它与我的非Flask项目逻辑连接,但是仅限于Flask。我完全不知道这是否是好设计,但有没有办法从一个蓝图传播两个模板文件夹?并使这两个模板设置整个Flask项目?
注意: formgear
是我的非Flask项目的名称。
这里是调查的踪迹。
.blueprints b
$ b
class Blueprint(_PackageBoundObject):
....
def __init __(self,name ,import_name,static_folder = None,
static_url_path = None,template_folder = None,
...)
_PackageBoundObject .__ init __(self,import_name,template_folder)
....
从flask.helpers
@locked_cached_property
def jinja_loader(self):
这个包绑定对象的Jinja加载器
.. versionadded :: 0.5
如果self.template_folder不是无:
返回FileSystemLoader(os.path.join(self.root_path,
$ b 调查结果:
$ b ol>
传递给 Blueprints
的template_folder随后是类 _PackageBoundObject
它被视为单个字符串而不是文件夹列表
结果:
- 不能为template_folder传递多个路径
I learned how to create Flask Blueprints and can create blueprint for non flask products that uses Jinja2 templates and use them inside flask projects. I do something like this:
# blueprint code
from flask import Blueprint
from pkg_resources import resource_filename
app = Blueprint('formgear', __name__,
template_folder=resource_filename('formgear', 'templates'))
And now I want to add another set of templates, which is logically connected with my non-Flask project but are Flask-only specific. I'm completely not sure is it good desing, but is there any way to propagate both templates folders from one blueprint? And make both templates set available for whole Flask project?
Note: formgear
is name of my non-Flask project.
解决方案 Here is the trail of investigations.
From flask.blueprints
class Blueprint(_PackageBoundObject):
....
def __init__(self, name, import_name, static_folder=None,
static_url_path=None, template_folder=None,
...)
_PackageBoundObject.__init__(self, import_name, template_folder)
....
From flask.helpers
@locked_cached_property
def jinja_loader(self):
"""The Jinja loader for this package bound object.
.. versionadded:: 0.5
"""
if self.template_folder is not None:
return FileSystemLoader(os.path.join(self.root_path,
self.template_folder))
Investigation Result:
- template_folder which is passed to
Blueprints
is subsequently to class _PackageBoundObject
- It is treated as a single string and not as a list of folders
Result:
- You can not pass multiple paths for template_folder
这篇关于如何从Flask蓝图的多个template_folder加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!