class documentation

class PackageLoader(BaseLoader):

View In Hierarchy

Load templates from a directory in a Python package.

The following example looks up templates in the pages directory within the project.ui package.

loader = PackageLoader("project.ui", "pages")

Only packages installed as directories (standard pip behavior) or zip/egg files (less common) are supported. The Python API for introspecting data in packages is too limited to support other installation methods the way this loader requires.

There is limited support for PEP 420 namespace packages. The template directory is assumed to only be in one namespace contributor. Zip files contributing to a namespace are not supported.

Changed in version 3.0: No longer uses setuptools as a dependency.
Changed in version 3.0: Limited PEP 420 namespace package support.
Parameters
package​_nameImport name of the package that contains the template directory.
package​_pathDirectory within the imported package that contains the templates.
encodingEncoding of template files.
Method __init__ Undocumented
Method get​_source No summary
Method list​_templates Iterates over all templates. If the loader does not support that it should raise a TypeError which is the default behavior.
Instance Variable ​_archive Undocumented
Instance Variable ​_loader Undocumented
Instance Variable ​_template​_root Undocumented
Instance Variable encoding Undocumented
Instance Variable package​_name Undocumented
Instance Variable package​_path Undocumented

Inherited from BaseLoader:

Method load No summary
Class Variable has​_source​_access Undocumented
def __init__(self, package_name, package_path='templates', encoding='utf-8'):

Undocumented

Parameters
package​_name:strUndocumented
package​_path:strUndocumented
encoding:strUndocumented
def get_source(self, environment, template):

Get the template source, filename and reload helper for a template. It's passed the environment and template name and has to return a tuple in the form (source, filename, uptodate) or raise a TemplateNotFound error if it can't locate the template.

The source part of the returned tuple must be the source of the template as a string. The filename should be the name of the file on the filesystem if it was loaded from there, otherwise None. The filename is used by Python for the tracebacks if no loader extension is used.

The last item in the tuple is the uptodate function. If auto reloading is enabled it's always called to check if the template changed. No arguments are passed so the function must store the old state somewhere (for example in a closure). If it returns False the template will be reloaded.

Parameters
environment:EnvironmentUndocumented
template:strUndocumented
Returns
t.Tuple[str, str, t.Optional[t.Callable[[], bool]]]Undocumented
def list_templates(self):
Iterates over all templates. If the loader does not support that it should raise a TypeError which is the default behavior.
Returns
t.List[str]Undocumented
_archive =

Undocumented

_loader =

Undocumented

_template_root =

Undocumented

encoding =

Undocumented

package_name =

Undocumented

package_path =

Undocumented