How to load from more then one template_folder for Flask blueprint?

帅比萌擦擦* 提交于 2019-12-06 22:18:48

问题


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.


回答1:


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:

  1. template_folder which is passed to Blueprints is subsequently to class _PackageBoundObject
  2. It is treated as a single string and not as a list of folders

Result:

  1. You can not pass multiple paths for template_folder


来源:https://stackoverflow.com/questions/11192796/how-to-load-from-more-then-one-template-folder-for-flask-blueprint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!