Skip to content

Tokens

liquid.Token

Bases: NamedTuple

A substring into Liquid template source text along with its type.

kind instance-attribute

kind: str

The type of the token.

Could be one of TOKEN_TAG, TOKEN_OUTPUT, TOKEN_EXPRESSION or TOKEN_CONTENT indicating that it is a "top-level" token. All other tokens kinds are for tag and output expressions.

source instance-attribute

source: str

A reference to the template source text from which this token came.

start_index instance-attribute

start_index: int

The index into source where this token starts.

value instance-attribute

value: str

The substring associated with this token.

is_tag

is_tag(name: str) -> bool

Return True if this token is a tag and has the given name.

test

test(typ: str) -> bool

Return True if this token's type matches the typ argument.

liquid.TokenStream

Step through a sequence of tokens.

current property

current: Token

Return the token at the head of the stream without advancing.

peek property

peek: Token

Look at the next token.

eat

eat(typ: str) -> Token

Consume and return the next token.

If the type of the next token is equal to typ, raise an exceptions.

This is equivalent to stream.expect(typ) followed by next(stream).

eat_one_of

eat_one_of(*typ: str) -> Token

Consume and return the next token.

If the type of the next token is equal to typ, raise an exceptions.

This is equivalent to stream.expect(typ) followed by next(stream).

expect

expect(typ: str, value: Optional[str] = None) -> Token

Check the current token in the stream matches the given type and value.

Returns the current token if its type matches typ. Raises a LiquidSyntaxError if it doesn't.

expect_eos

expect_eos() -> None

Raise a syntax error if we're not at the end of the stream.

expect_peek

expect_peek(typ: str, value: Optional[str] = None) -> Token

Check the next token in the stream matches the given type and value.

Returns the next token if its type matches typ. Raises a LiquidSyntaxError it doesn't.

into_inner

into_inner(
    *, tag: Optional[Token] = None, eat: bool = True
) -> TokenStream

Return a stream of tokens for the current expression token.

If the current token is not an expression, a LiquidSyntaxError is raised.

If tag is given, it will be used to add context information to the syntax error, should one be raised.

If eat is true (the default), the current token is consumed.

next

next() -> Token

Return the next token and advance the iterator.

next_token

next_token() -> Token

Return the next token and advance the iterator.