Skip to content

Loaders

liquid2.loader.BaseLoader

Bases: ABC

Base class for all template loaders.

get_source abstractmethod

get_source(
    env: Environment,
    template_name: str,
    *,
    context: RenderContext | None = None,
    **kwargs: object
) -> TemplateSource

Get source information for a template.

PARAMETER DESCRIPTION
env

The Environment attempting to load the template source text.

TYPE: Environment

template_name

A name or identifier for a template's source text.

TYPE: str

context

An optional render context that can be used to narrow the template source search space.

TYPE: RenderContext | None DEFAULT: None

kwargs

Arbitrary arguments that can be used to narrow the template source search space.

TYPE: object DEFAULT: {}

get_source_async async

get_source_async(
    env: Environment,
    template_name: str,
    *,
    context: RenderContext | None = None,
    **kwargs: object
) -> TemplateSource

An async version of get_source.

The default implementation delegates to get_source().

load

load(
    env: Environment,
    name: str,
    *,
    globals: Mapping[str, object] | None = None,
    context: RenderContext | None = None,
    **kwargs: object
) -> Template

Find and parse template source code.

PARAMETER DESCRIPTION
env

The Environment attempting to load the template source text.

TYPE: Environment

name

A name or identifier for a template's source text.

TYPE: str

globals

A mapping of render context variables attached to the resulting template.

TYPE: Mapping[str, object] | None DEFAULT: None

context

An optional render context that can be used to narrow the template source search space.

TYPE: RenderContext | None DEFAULT: None

kwargs

Arbitrary arguments that can be used to narrow the template source search space.

TYPE: object DEFAULT: {}

load_async async

load_async(
    env: Environment,
    name: str,
    *,
    globals: Mapping[str, object] | None = None,
    context: RenderContext | None = None,
    **kwargs: object
) -> Template

An async version of load().

liquid2.loader.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: str

name

The liquid template file name or other string identifying its origin.

TYPE: str

uptodate

Optional callable that will return True if the template is up to date, or False if it needs to be reloaded.

TYPE: Callable[[], bool] | Callable[[], Awaitable[bool]] | None

matter

Optional mapping containing variables associated with the template. Could be "front matter" or other meta data.

TYPE: dict[str, object] | None

liquid2.FileSystemLoader

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: str | Path | Iterable[str | Path]

encoding

Encoding to use when opening files.

TYPE: str DEFAULT: 'utf-8'

ext

A default file extension. Should include a leading period.

TYPE: str | None DEFAULT: None

get_source

get_source(
    env: Environment,
    template_name: str,
    *,
    context: RenderContext | None = None,
    **kwargs: object
) -> TemplateSource

Get source information for a template.

get_source_async async

get_source_async(
    env: Environment,
    template_name: str,
    *,
    context: RenderContext | None = None,
    **kwargs: object
) -> TemplateSource

Get source information for a template.

resolve_path

resolve_path(template_name: str) -> Path

Return a path to the template identified by template_name.

If the search path is a list of paths, returns the first path where template_name exists. If none of the search paths contain template_name, a TemplateNotFound exception is raised.

liquid2.CachingFileSystemLoader

Bases: CachingLoaderMixin, FileSystemLoader

A file system loader that caches parsed templates in memory.

PARAMETER DESCRIPTION
search_path

One or more paths to search.

TYPE: Union[str, Path, Iterable[Union[str, Path]]]

encoding

Open template files with the given encoding.

TYPE: str DEFAULT: 'utf-8'

ext

A default file extension. Should include a leading period.

TYPE: str | None DEFAULT: None

auto_reload

If True, automatically reload a cached template if it has been updated.

TYPE: bool DEFAULT: True

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 uid, where uid is a unique identifier for a user and templates are arranged in folders named for each uid inside the search path.

TYPE: str DEFAULT: ''

capacity

The maximum number of templates to hold in the cache before removing the least recently used template.

TYPE: int DEFAULT: 300

liquid2.DictLoader

Bases: BaseLoader

A loader that loads templates from a dictionary.

PARAMETER DESCRIPTION
templates

A dictionary mapping template names to template source strings.

TYPE: dict[str, str]

get_source

get_source(
    env: Environment,
    template_name: str,
    *,
    context: RenderContext | None = None,
    **kwargs: object
) -> TemplateSource

Get the source, filename and reload helper for a template.

liquid2.CachingDictLoader

Bases: CachingLoaderMixin, DictLoader

A DictLoader that caches parsed templates in memory.

liquid2.PackageLoader

Bases: BaseLoader

A template loader that reads templates from Python packages.

PARAMETER DESCRIPTION
package

Import name of a package containing Liquid templates.

TYPE: str | ModuleType

package_path

One or more directories in the package containing Liquid templates.

TYPE: str | Iterable[str] DEFAULT: 'templates'

encoding

Encoding of template files.

TYPE: str DEFAULT: 'utf-8'

ext

A default file extension to use if one is not provided. Should include a leading period.

TYPE: str DEFAULT: '.liquid'

get_source

get_source(
    env: Environment,
    template_name: str,
    *,
    context: RenderContext | None = None,
    **kwargs: object
) -> TemplateSource

Get source information for a template.

get_source_async async

get_source_async(
    env: Environment,
    template_name: str,
    *,
    context: RenderContext | None = None,
    **kwargs: object
) -> TemplateSource

Get source information for a template.

liquid2.ChoiceLoader

Bases: BaseLoader

A template loader that delegates to other template loaders.

PARAMETER DESCRIPTION
loaders

A list of loaders implementing liquid.loaders.BaseLoader.

TYPE: list[BaseLoader]

get_source

get_source(
    env: Environment,
    template_name: str,
    *,
    context: RenderContext | None = None,
    **kwargs: object
) -> TemplateSource

Get source information for a template.

get_source_async async

get_source_async(
    env: Environment,
    template_name: str,
    *,
    context: RenderContext | None = None,
    **kwargs: object
) -> TemplateSource

Get source information for a template.

liquid2.CachingChoiceLoader

Bases: CachingLoaderMixin, ChoiceLoader

A ChoiceLoader that caches parsed templates in memory.

PARAMETER DESCRIPTION
loaders

A list of loaders implementing liquid.loaders.BaseLoader.

TYPE: list[BaseLoader]

auto_reload

If True, automatically reload a cached template if it has been updated.

TYPE: bool DEFAULT: True

namespace_key

The name of a global render context variable or loader keyword argument that resolves to the current loader "namespace" or "scope".

TYPE: str DEFAULT: ''

capacity

The maximum number of templates to hold in the cache before removing the least recently used template.

TYPE: int DEFAULT: 300

liquid2.CachingLoaderMixin

Bases: ABC, _CachingLoaderProtocol

A mixin class that adds caching to a template loader.