Skip to content

Render context

liquid2.RenderContext

Template render state.

assign

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

Add key to the local namespace with value val.

copy

copy(
    token: TokenT,
    *,
    namespace: Mapping[str, object],
    template: Template | None = None,
    disabled_tags: set[str] | None = None,
    carry_loop_iterations: bool = False,
    block_scope: bool = False
) -> RenderContext

Return a copy of this render context with a new scope.

cycle

cycle(cycle_hash: int, length: int) -> int

Return the index of the next item in the named cycle.

decrement

decrement(name: str) -> int

Decrement the named counter and return its value.

extend

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

Extend this context with the given read-only namespace.

filter

filter(
    name: str, *, token: TokenT
) -> Callable[..., object]

Return the filter callable for name.

get

get(
    path: list[object],
    *,
    token: TokenT | None,
    default: object = UNDEFINED
) -> object

Resolve the variable path in the current namespace.

get_async async

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

Asynchronously resolve the variable path in the current namespace.

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_output_buffer

get_output_buffer(parent_buffer: TextIO | None) -> StringIO

Return a new output buffer respecting any limits set on the environment.

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.

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.

markup

markup(s: str) -> str | Markup

Return a safe string if auto escape is enabled.

parentloop

parentloop(token: TokenT) -> 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, default: object = UNDEFINED) -> object

Resolve variable name in the current scope.

stopindex

stopindex(key: str, index: int | None = None) -> int

Set or return the stop index of a for loop.