Skip to content

Render context

liquid.RenderContext

A template render context.

A new render context is created automatically each time BoundTemplate.render is called, which includes globals set on the bound liquid.Environment and liquid.template.BoundTemplate.

assign

assign(key: str, val: Any) -> None

Add or replace the context variable named key with the value val.

copy

copy(
    namespace: Mapping[str, object],
    disabled_tags: Optional[list[str]] = None,
    carry_loop_iterations: bool = False,
    template: Optional[BoundTemplate] = None,
    block_scope: bool = False,
) -> RenderContext

Return a copy of this render context.

Local variables and other state for stateful tags are not copied.

cycle

cycle(key: object, length: int) -> int

Return the index of the next item in the cycle.

decrement

decrement(name: str) -> int

Decrement the named counter and return its value.

error

error(exc: LiquidError) -> None

Ignore, raise or convert the given exception to a warning.

extend

extend(
    namespace: Mapping[str, object],
    template: Optional[BoundTemplate] = None,
) -> Iterator[RenderContext]

Extend this context with the given read-only namespace.

filter

filter(
    name: str, token: Optional[Token]
) -> Callable[..., object]

Return the filter function with given name.

get

get(
    path: list[object],
    *,
    token: Optional[Token],
    default: object = UNDEFINED
) -> object

Resolve and return path in the current scope.

If token is not None, it will be used to give error messages extra contextual information.

Returns default is the path is not in scope.

get_async async

get_async(
    path: list[object],
    *,
    token: Optional[Token],
    default: object = UNDEFINED
) -> object

Resolve and return path in the current scope.

If token is not None, it will be used to give error messages extra contextual information.

Returns default is the path is not in scope.

get_buffer

get_buffer(buf: Optional[TextIO] = None) -> StringIO

Return a new StringIO object that respects the configured stream limit.

get_item

get_item(obj: Any, key: Any) -> Any

An item getter used when resolving a Liquid path.

Override this to change the behavior of .first, .last and .size.

get_item_async async

get_item_async(obj: Any, key: Any) -> Any

An async item getter for resolving paths.

get_size_of_locals

get_size_of_locals() -> int

Return the "size" or a "score" for the current local namespace.

This is used by the optional local namespace resource limit. Override get_size_of_locals to customize how the limit is calculated. Be sure to consider self.local_namespace_size_carry when writing a custom implementation of get_size_of_locals.

The default implementation uses sys.getsizeof() on each of the local namespace's values. It is not a reliable measure of size in bytes.

get_template

get_template(name: str) -> BoundTemplate

Load a template from the environment.

get_template_async async

get_template_async(name: str) -> BoundTemplate

Load a template from the environment asynchronously.

ifchanged

ifchanged(val: str) -> bool

Return True if the ifchanged value has changed.

increment

increment(name: str) -> int

Increment the named counter and return its value.

loop

loop(
    namespace: Mapping[str, object], forloop: ForLoop
) -> Iterator[RenderContext]

Just like Context.extend, but keeps track of ForLoop objects too.

parentloop

parentloop() -> Union[Undefined, object]

Return the last ForLoop object from the loop stack.

raise_for_loop_limit

raise_for_loop_limit(length: int = 1) -> None

Raise a LoopIterationLimitError if loop stack is bigger than the limit.

resolve

resolve(
    name: str,
    *,
    token: Optional[Token] = None,
    default: object = UNDEFINED
) -> Any

Return the object/value at name in the current scope.

This is like get, but does a single, top-level lookup rather than a chained lookup from a sequence of keys.

stopindex

stopindex(key: str, index: Optional[int] = None) -> int

Set or return the stop index of a for loop.