Rendering Templates
Environment
Environment(
tag_start_string: str = "{%",
tag_end_string: str = "%}",
statement_start_string: str = "{{",
statement_end_string: str = "}}",
strip_tags: bool = False,
tolerance: Mode = Mode.STRICT,
loader: Optional[loaders.BaseLoader] = None,
undefined: Type[Undefined] = Undefined,
strict_filters: bool = True,
autoescape: bool = False,
auto_reload: bool = True,
cache_size: int = 300,
globals: Optional[Mapping[str, object]] = None,
template_comments: bool = False,
comment_start_string: str = "{#",
comment_end_string: str = "#}",
expression_cache_size: int = 0,
)
Shared configuration from which templates can be loaded and parsed.
An Environment
is where you might register custom tags and filters, or store
global context variables that should be included with every template.
PARAMETER | DESCRIPTION |
---|---|
tag_start_string |
The sequence of characters indicating the start of a liquid tag.
TYPE:
|
tag_end_string |
The sequence of characters indicating the end of a liquid tag.
TYPE:
|
statement_start_string |
The sequence of characters indicating the start of an output statement.
TYPE:
|
statement_end_string |
The sequence of characters indicating the end of an output statement.
TYPE:
|
template_comments |
If
TYPE:
|
comment_start_string |
The sequence of characters indicating the start of a
comment.
TYPE:
|
comment_end_string |
The sequence of characters indicating the end of a
comment.
TYPE:
|
tolerance |
Indicates how tolerant to be of errors. Must be one of
TYPE:
|
loader |
A template loader. If you want to use the builtin "render" or "include" tags, a loader must be configured.
TYPE:
|
undefined |
A subclass of |
strict_filters |
If
TYPE:
|
autoescape |
If
TYPE:
|
auto_reload |
If
TYPE:
|
cache_size |
The capacity of the template cache in number of templates.
If
TYPE:
|
expression_cache_size |
The capacity of each of the common expression caches.
A
TYPE:
|
globals |
An optional mapping that will be added to the render context of any template loaded from this environment.
TYPE:
|
ATTRIBUTE | DESCRIPTION |
---|---|
context_depth_limit |
Class attribute. The maximum number of times a render
context can be extended or wrapped before a
TYPE:
|
local_namespace_limit |
Class attribute. The maximum number of bytes (according
to sys.getsizeof) allowed in a template's local namespace, per render,
before a
TYPE:
|
loop_iteration_limit |
Class attribute. The maximum number of loop iterations
allowed before a
TYPE:
|
output_stream_limit |
Class attribute. The maximum number of bytes that can be
written to a template's output stream, per render, before an
TYPE:
|
render_whitespace_only_blocks |
Class attribute. Indicates if block tags that,
when rendered, contain whitespace only should be output. Defaults to
TYPE:
|
undefined |
The undefined type. When an identifier can not be resolved, an
instance of
TYPE:
|
strict_filters |
Indicates if an undefined filter should raise an exception or be ignored.
TYPE:
|
autoescape |
Indicates if auto-escape is enabled.
TYPE:
|
tags |
A dictionary mapping tag names to
TYPE:
|
filters |
A dictionary mapping filter names to callable objects implementing a filter's behavior.
TYPE:
|
mode |
The current tolerance mode.
TYPE:
|
cache |
The template cache.
TYPE:
|
auto_reload |
Indicates if automatic reloading of templates is enabled.
TYPE:
|
template_class |
TYPE:
|
globals |
A dictionary of variables that will be added to the context of every template rendered from the environment.
TYPE:
|
add_filter
Register a filter function with the environment.
PARAMETER | DESCRIPTION |
---|---|
name |
The filter's name. Does not need to match the function name. This is what you'll use to apply the filter to an expression in a liquid template.
TYPE:
|
func |
Any callable that accepts at least one argument, the result of the
expression the filter is applied to. If the filter needs access to the
active environment or render context, use
TYPE:
|
add_tag
Register a liquid tag with the environment.
Built-in tags are registered for you automatically with every new Environment
.
PARAMETER | DESCRIPTION |
---|---|
tag |
The tag to register.
TYPE:
|
analyze_tags
analyze_tags(
name: str,
*,
context: Optional["Context"] = None,
inner_tags: Optional[InnerTagMap] = None,
**kwargs: str
) -> TagAnalysis
Audit template tags without parsing source text into an abstract syntax tree.
This is useful for identifying unknown, misplaced and unbalanced tags in a
template's source text. See also liquid.template.BoundTemplate.analyze
.
PARAMETER | DESCRIPTION |
---|---|
name |
The template's name or identifier, as you would use with
TYPE:
|
context |
An optional render context the loader might use to modify the
template search space. If given, uses
TYPE:
|
inner_tags |
A mapping of block tags to a list of allowed "inner" tags for
the block. For example,
TYPE:
|
kwargs |
Loader context.
TYPE:
|
analyze_tags_async
async
analyze_tags_async(
name: str,
*,
context: Optional["Context"] = None,
inner_tags: Optional[InnerTagMap] = None,
**kwargs: str
) -> TagAnalysis
An async version of Environment.analyze_tags
.
analyze_tags_from_string
analyze_tags_from_string(
source: str,
name: str = "<string>",
*,
inner_tags: Optional[InnerTagMap] = None
) -> TagAnalysis
Analyze tags in template source text.
Unlike template static or contextual analysis, a tag audit does not parse the
template source text into an AST, nor does it attempt to load partial templates
from {% include %}
or {% render %}
tags.
PARAMETER | DESCRIPTION |
---|---|
source |
The source text of the template.
TYPE:
|
name |
A name or identifier for the template.
TYPE:
|
inner_tags |
A mapping of block tags to a list of allowed "inner" tags for
the block. For example,
TYPE:
|
error
error(
exc: Union[Type[Error], Error],
msg: Optional[str] = None,
linenum: Optional[int] = None,
) -> None
Raise, warn or ignore the given exception according to the current mode.
from_string
from_string(
source: str,
name: str = "",
path: Optional[Union[str, Path]] = None,
globals: Optional[Mapping[str, object]] = None,
matter: Optional[Mapping[str, object]] = None,
) -> BoundTemplate
Parse the given string as a liquid template.
PARAMETER | DESCRIPTION |
---|---|
source |
The liquid template source code.
TYPE:
|
name |
Optional name of the template. Available as
TYPE:
|
path |
Optional path or identifier to the origin of the template.
TYPE:
|
globals |
An optional mapping of render context variables attached to the resulting template.
TYPE:
|
matter |
Optional mapping of render context variables associated with the template. Could be "front matter" or other meta data.
TYPE:
|
get_template
Load and parse a template using the configured loader.
PARAMETER | DESCRIPTION |
---|---|
name |
The template's name. The loader is responsible for interpreting the name. It could be the name of a file or some other identifier.
TYPE:
|
globals |
A mapping of render context variables attached to the resulting template.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
TemplateNotFound
|
If a template with the given name can not be found. |
get_template_async
async
An async version of get_template
.
get_template_with_args
get_template_with_args(
name: str,
globals: Optional[Mapping[str, object]] = None,
**kwargs: object
) -> BoundTemplate
Load and parse a template with arbitrary loader arguments.
This method bypasses the environment's template cache. You should use a caching loader instead when the loader requires extra keyword arguments.
New in version 1.9.0.
get_template_with_args_async
async
get_template_with_args_async(
name: str,
globals: Optional[Mapping[str, object]] = None,
**kwargs: object
) -> BoundTemplate
An async version of get_template_with_args
.
New in version 1.9.0.
get_template_with_context
Load and parse a template with reference to a render context.
This method bypasses the environment's template cache. You should consider using a caching loader.
get_template_with_context_async
async
An async version of get_template_with_context
.
make_globals
Combine environment globals with template globals.
parse
Parse source as a Liquid template.
More often than not you'll want to use Environment.from_string
instead.
set_expression_cache_size
Create or replace cached versions of the common expression parsers.
If maxsize
is less than 1
, no expression caching will happen.
PARAMETER | DESCRIPTION |
---|---|
maxsize |
The maximum size of each expression cache.
TYPE:
|
setup_tags_and_filters
Add default tags and filters to this environment.
Template
Template(
source: str,
tag_start_string: str = "{%",
tag_end_string: str = "%}",
statement_start_string: str = "{{",
statement_end_string: str = "}}",
strip_tags: bool = False,
tolerance: Mode = Mode.STRICT,
undefined: Type[Undefined] = Undefined,
strict_filters: bool = True,
autoescape: bool = False,
auto_reload: bool = True,
cache_size: int = 300,
globals: Optional[Mapping[str, object]] = None,
template_comments: bool = False,
comment_start_string: str = "{#",
comment_end_string: str = "#}",
expression_cache_size: int = 0,
) -> BoundTemplate
Parse a template, automatically creating an Environment
to bind it to.
Other than source
, all arguments are passed to the implicit Environment
,
which might have been cached from previous calls to Template
.
PARAMETER | DESCRIPTION |
---|---|
tag_start_string |
The sequence of characters indicating the start of a liquid tag.
TYPE:
|
tag_end_string |
The sequence of characters indicating the end of a liquid tag.
TYPE:
|
statement_start_string |
The sequence of characters indicating the start of an output statement.
TYPE:
|
statement_end_string |
The sequence of characters indicating the end of an output statement.
TYPE:
|
strip_tags |
Has no effect. We don't support automatic whitespace stripping.
TYPE:
|
template_comments |
If
TYPE:
|
comment_start_string |
The sequence of characters indicating the start of a
comment.
TYPE:
|
comment_end_string |
The sequence of characters indicating the end of a
comment.
TYPE:
|
tolerance |
Indicates how tolerant to be of errors. Must be one of
TYPE:
|
undefined |
A subclass of |
strict_filters |
If
TYPE:
|
autoescape |
If
TYPE:
|
auto_reload |
If
TYPE:
|
cache_size |
The capacity of the template cache in number of templates.
If
TYPE:
|
expression_cache_size |
The capacity of each of the common expression caches.
A
TYPE:
|
globals |
An optional mapping that will be added to the render context of any template loaded from this environment.
TYPE:
|
BoundTemplate
BoundTemplate(
env: Environment,
parse_tree: ParseTree,
name: str = "",
path: Optional[Union[str, Path]] = None,
globals: Optional[Dict[str, object]] = None,
matter: Optional[Mapping[str, object]] = None,
uptodate: UpToDate = None,
)
A liquid template that has been parsed and is bound to a liquid.Environment
.
You probably don't want to instantiate BoundTemplate
directly. Use
liquid.Environment.from_string()
or liquid.Environment.get_template()
instead.
PARAMETER | DESCRIPTION |
---|---|
env |
The environment this template is bound to.
TYPE:
|
parse_tree |
The parse tree representing this template.
TYPE:
|
name |
Optional name of the template. Defaults to an empty string.
TYPE:
|
path |
Optional origin path or identifier for the template.
TYPE:
|
globals |
An optional mapping of context variables made available every
time the resulting template is rendered. Defaults to
TYPE:
|
matter |
Optional mapping containing variables associated with the template. Could be "front matter" or other meta data.
TYPE:
|
uptodate |
Optional callable that will return
TYPE:
|
analyze
Statically analyze this template and any included/rendered templates.
PARAMETER | DESCRIPTION |
---|---|
follow_partials |
If
TYPE:
|
raise_for_failures |
If
TYPE:
|
analyze_async
async
An async version of analyze
.
analyze_with_context
Analyze a path through this template's syntax tree given some context data.
Unlike analyze
, this form of template analysis does perform a render,
capturing template variables as we go.
Python Liquid does not currently support template introspection from within a
render context or Expression
object. Meaning line numbers and template names
are not available. Only variable names are reported along with the number of
times they were referenced.
It's also, using this method, not currently possible to detect names added to a
block's scope. For example, forloop.index
will be included in the results
object if referenced within a for
loop block.
Accepts the same arguments as render
.
analyze_with_context_async
async
An async version of analyze_with_context
.
is_up_to_date_async
async
An async version of the is_up_to_date
property.
If template.uptodate
is a coroutine, it wil be awaited. Otherwise it will be
called just like is_up_to_date
make_globals
Return a mapping including render arguments and template globals.
make_partial_namespace
Return a namespace dictionary.
This is used by render_with_context
to extend an existing context.
render
Render the template with args
and kwargs
included in the render context.
Accepts the same arguments as the dict
constructor.
render_async
async
An async version of liquid.template.BoundTemplate.render
.
render_with_context
render_with_context(
context: Context,
buffer: TextIO,
*args: Any,
partial: bool = False,
block_scope: bool = False,
**kwargs: Any
) -> None
Render the template using an existing context and output buffer.
PARAMETER | DESCRIPTION |
---|---|
context |
A render context.
TYPE:
|
buffer |
File-like object to which rendered text is written.
TYPE:
|
partial |
If
TYPE:
|
block_scope |
If
TYPE:
|
args |
Passed to the
TYPE:
|
kwargs |
Passed to the
TYPE:
|