Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 399
Mikhail Kashkin Avatar asked Jun 25 '12 15:06

Mikhail Kashkin


1 Answers

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
like image 145
pyfunc Avatar answered Sep 28 '22 12:09

pyfunc