Template Loaders
CachingFileSystemLoader
CachingFileSystemLoader(
search_path: Union[
str, Path, Iterable[Union[str, Path]]
],
encoding: str = "utf-8",
ext: str = ".liquid",
*,
auto_reload: bool = True,
namespace_key: str = "",
cache_size: int = 300
)
Bases: CachingLoaderMixin
, FileExtensionLoader
A file system loader that caches parsed templates in memory.
PARAMETER | DESCRIPTION |
---|---|
search_path |
One or more paths to search.
TYPE:
|
encoding |
Open template files with the given encoding.
TYPE:
|
ext |
A default file extension. Should include a leading period.
TYPE:
|
auto_reload |
If
TYPE:
|
namespace_key |
The name of a global render context variable or loader keyword argument that resolves to the current loader "namespace" or "scope". If you're developing a multi-user application, a good namespace might be
TYPE:
|
cache_size |
The maximum number of templates to hold in the cache before removing the least recently used template.
TYPE:
|
FileSystemLoader
FileSystemLoader(
search_path: Union[
str, Path, Iterable[Union[str, Path]]
],
encoding: str = "utf-8",
)
Bases: BaseLoader
A loader that loads templates from one or more directories on the file system.
PARAMETER | DESCRIPTION |
---|---|
search_path |
One or more paths to search.
TYPE:
|
encoding |
Open template files with the given encoding.
TYPE:
|
FileExtensionLoader
FileExtensionLoader(
search_path: Union[
str, Path, Iterable[Union[str, Path]]
],
encoding: str = "utf-8",
ext: str = ".liquid",
)
Bases: FileSystemLoader
A file system loader that adds a file name extension if one is missing.
PARAMETER | DESCRIPTION |
---|---|
search_path |
One or more paths to search.
TYPE:
|
encoding |
Open template files with the given encoding.
TYPE:
|
ext |
A default file extension. Should include a leading period.
TYPE:
|
DictLoader
Bases: BaseLoader
A loader that loads templates from a dictionary.
PARAMETER | DESCRIPTION |
---|---|
templates |
A dictionary mapping template names to template source strings.
TYPE:
|
ChoiceLoader
Bases: BaseLoader
A template loader that will try each of a list of loaders in turn.
PARAMETER | DESCRIPTION |
---|---|
loaders |
A list of loaders implementing
TYPE:
|
get_source
Get source code for a template from one of the configured loaders.
get_source_async
async
An async version of get_source
.
get_source_with_args
Get source code for a template from one of the configured loaders.
get_source_with_args_async
async
get_source_with_args_async(
env: Environment, template_name: str, **kwargs: object
) -> TemplateSource
An async version of get_source_with_args
.
get_source_with_context
Get source code for a template from one of the configured loaders.
CachingChoiceLoader
CachingChoiceLoader(
loaders: List[BaseLoader],
*,
auto_reload: bool = True,
namespace_key: str = "",
cache_size: int = 300
)
Bases: CachingLoaderMixin
, ChoiceLoader
A ChoiceLoader
that caches parsed templates in memory.
PARAMETER | DESCRIPTION |
---|---|
loaders |
A list of loaders implementing
TYPE:
|
auto_reload |
If
TYPE:
|
namespace_key |
The name of a global render context variable or loader keyword argument that resolves to the current loader "namespace" or "scope".
TYPE:
|
cache_size |
The maximum number of templates to hold in the cache before removing the least recently used template.
TYPE:
|
New in version 1.11.0.
PackageLoader
PackageLoader(
package: Union[str, ModuleType],
*,
package_path: Union[str, Iterable[str]] = "templates",
encoding: str = "utf-8",
ext: str = ".liquid"
)
Bases: BaseLoader
A template loader that reads templates from Python packages.
PARAMETER | DESCRIPTION |
---|---|
package |
Import name of a package containing Liquid templates.
TYPE:
|
package_path |
One or more directories in the package containing Liquid templates.
TYPE:
|
encoding |
Encoding of template files.
TYPE:
|
ext |
A default file extension to use if one is not provided. Should include a leading period.
TYPE:
|
New in version 1.11.0.
BaseLoader
Bases: ABC
Base template loader from which all template loaders are derived.
ATTRIBUTE | DESCRIPTION |
---|---|
caching_loader |
Indicates if this loader implements its own cache.
Setting this sto
TYPE:
|
get_source
Get the template source, filename and reload helper for a template.
PARAMETER | DESCRIPTION |
---|---|
env |
The
TYPE:
|
template_name |
A name or identifier for a template's source text.
TYPE:
|
get_source_async
async
An async version of get_source
.
The default implementation delegates to get_source()
.
get_source_with_args
Get template source text, optionally referencing arbitrary keyword arguments.
Keyword arguments can be useful for multi-user environments where you need to modify a template loader's search space for a given user.
By default, this method delegates to get_source
, ignoring any keyword
arguments.
New in version 1.9.0.
get_source_with_args_async
async
get_source_with_args_async(
env: Environment, template_name: str, **kwargs: object
) -> TemplateSource
An async version of get_source_with_args
.
New in version 1.9.0.
get_source_with_context
Get a template's source, optionally referencing a render context.
get_source_with_context_async
async
get_source_with_context_async(
context: Context, template_name: str, **kwargs: str
) -> TemplateSource
An async version of get_source_with_context
.
load
Find and parse template source code.
This is used internally by liquid.Environment
to load template
source text. load()
delegates to BaseLoader.get_source()
. Custom
loaders would typically implement get_source()
rather than overriding
load()
.
load_async
async
An async version of load()
.
load_with_args
load_with_args(
env: Environment,
name: str,
globals: TemplateNamespace = None,
**kwargs: object
) -> BoundTemplate
Load template source text, optionally referencing extra keyword arguments.
Most custom loaders will want to override get_source_with_args()
rather than
this method. For example, you might want to override load_with_args()
and
get_source_with_args()
when implementing a custom caching loader. Where cache
handling happens in load_*
methods.
load_with_args_async
async
load_with_args_async(
env: Environment,
name: str,
globals: TemplateNamespace = None,
**kwargs: object
) -> BoundTemplate
An async version of load_with_args()
.
load_with_context
Load and parse a template, optionally referencing a render context.
TemplateSource
Bases: NamedTuple
A Liquid template source as returned by the get_source
method of a loader
.
ATTRIBUTE | DESCRIPTION |
---|---|
source |
The liquid template source code.
TYPE:
|
filename |
The liquid template file name or other string identifying its origin.
TYPE:
|
uptodate |
Optional callable that will return
TYPE:
|
matter |
Optional mapping containing variables associated with the template. Could be "front matter" or other meta data.
TYPE:
|
make_choice_loader
make_choice_loader(
loaders: List[BaseLoader],
*,
auto_reload: bool = True,
namespace_key: str = "",
cache_size: int = 300
) -> BaseLoader
A choice loader factory.
Returns one of CachingChoiceLoader
or ChoiceLoader
depending on the
given arguments.
A CachingChoiceLoader
is returned if cache_size > 0, otherwise a
ChoiceLoader
is returned.
auto_reload and namespace_key are ignored if cache_key is less than 1.
PARAMETER | DESCRIPTION |
---|---|
loaders |
A list of loaders implementing
TYPE:
|
auto_reload |
If
TYPE:
|
namespace_key |
The name of a global render context variable or loader keyword argument that resolves to the current loader "namespace" or "scope".
TYPE:
|
cache_size |
The maximum number of templates to hold in the cache before removing the least recently used template.
TYPE:
|
New in version 1.12.0
make_file_system_loader
make_file_system_loader(
search_path: Union[
str, Path, Iterable[Union[str, Path]]
],
*,
encoding: str = "utf-8",
ext: str = ".liquid",
auto_reload: bool = True,
namespace_key: str = "",
cache_size: int = 300
) -> BaseLoader
A file system template loader factory.
Returns one of CachingFileSystemLoader
, FileExtensionLoader
or
FileSystemLoader
depending in the given arguments.
A CachingFileSystemLoader
is returned if cache_size is greater than 0.
Otherwise a FileExtensionLoader
is returned if ext is not empty.
If ext is empty, a FileSystemLoader
is returned.
auto_reload and namespace_key are ignored if cache_key is less than 1.
PARAMETER | DESCRIPTION |
---|---|
search_path |
One or more paths to search.
TYPE:
|
encoding |
Open template files with the given encoding.
TYPE:
|
ext |
A default file extension. Should include a leading period.
TYPE:
|
auto_reload |
If
TYPE:
|
namespace_key |
The name of a global render context variable or loader keyword argument that resolves to the current loader "namespace" or "scope". If you're developing a multi-user application, a good namespace might be
TYPE:
|
cache_size |
The maximum number of templates to hold in the cache before removing the least recently used template.
TYPE:
|
New in version 1.12.0