Module documentation

ll.ul4c provides templating for XML/HTML as well as any other text-based format. A template defines placeholders for data output and basic logic (like loops and conditional blocks), that define how the final rendered output will look.

ll.ul4c compiles a template to an internal format, which makes it possible to implement template renderers in multiple programming languages.

ll.ul4c.withcontext(f)[source]

Normally when a function is exposed to UL4 this function will be called directly.

However when the function needs access to the rendering context (i.e. the local variables or information about the call stack), the function must have an additional first parameter, and UL4 must be told that this parmeter is required.

This can be done with this decorator.

exception ll.ul4c.LocationError[source]

Bases: Exception

Exception class that provides a location inside an UL4 template.

If an exception happens inside an UL4 template, this exception will propagate outwards and will be decorated with LocationError instances which will be chained via the __cause__ attribute. Only the original exception will be reraised again and again, so these LocationError will never have a traceback attached to them.

The first __cause__ attribute marks the location in the UL4 source where the exception happened and the last __cause__ attribute at the end of the exception chain marks the outermost call.

exception ll.ul4c.BlockError[source]

Bases: Exception

Exception that is raised by the compiler when an illegal block structure is detected (e.g. an <?end if?> without a previous <?if?>).

class ll.ul4c.Context[source]

Bases: object

A Context object stores the context of a call to a template. This consists of local, global and builtin variables and the indent stack.

class ll.ul4c.AST[source]

Bases: object

Base class for all UL4 syntax tree nodes.

eval(context)[source]

This evaluates the node.

For most nodes this is a normal function that returns the result of evaluating the node. (For these nodes the class attribute output is False.). For nodes that produce output (like literal text, PrintAST, PrintXAST or RenderAST etc.) it is a generator which yields the text output of the node. For blocks (which might contain nodes which produce output) this is also a generator. (For these nodes the class attribute output is True.)

class ll.ul4c.TextAST[source]

Bases: AST

AST node for literal text (i.e. the stuff between tags).

Attributes are:

textstr

The text

class ll.ul4c.IndentAST[source]

Bases: TextAST

AST node for literal text that is an indentation at the start of the line.

Attributes are:

textstr

The indentation text (i.e. a string that consists solely of whitespace).

class ll.ul4c.LineEndAST[source]

Bases: TextAST

AST node for literal text that is the end of a line.

Attributes are:

textstr

The text of the linefeed (i.e. "\n" or "\r\n").

class ll.ul4c.Tag[source]

Bases: AST

A Tag object is the location of a template tag in a template.

class ll.ul4c.CodeAST[source]

Bases: AST

The base class of all AST nodes that are not literal text.

These nodes appear inside a Tag.

class ll.ul4c.ConstAST[source]

Bases: CodeAST

AST node for a constant value.

Attributes are:

value

The constant to be loaded.

class ll.ul4c.SeqItemAST[source]

Bases: CodeAST

AST node for an item in a list/set “literal” (e.g. {x, y} or [x, y])

Attributes are:

valueAST

The list/set item (x and y in the above examples).

class ll.ul4c.UnpackSeqItemAST[source]

Bases: CodeAST

AST node for an * unpacking expression in a list/set “literal” (e.g. the y in {x, *y} or [x, *y])

Attributes are:

valueAST

The item to be unpacked into list/set items (y in the above examples).

class ll.ul4c.DictItemAST[source]

Bases: CodeAST

AST node for a dictionary entry in a dict expression (DictAST).

Attributes are:

keyAST

The key of the entry.

valueAST

The value of the entry.

class ll.ul4c.UnpackDictItemAST[source]

Bases: CodeAST

AST node for ** unpacking expressions in dict “literal” (e.g. the **u in {k: v, **u}).

Attributes are:

itemAST

The argument that must evaluate to a dictionary or an iterable of (key, value) pairs.

class ll.ul4c.PositionalArgumentAST[source]

Bases: CodeAST

AST node for a positional argument. (e.g. the x in f(x)).

Attributes are:

valueAST

The value of the argument (x in the above example).

class ll.ul4c.KeywordArgumentAST[source]

Bases: CodeAST

AST node for a keyword argument in a CallAST (e.g. the x=y in the function call f(x=y)).

Attributes are:

namestr

The keyword argument name ("x" in the above example).

valueAST

The keyword argument value (y in the above example).

class ll.ul4c.UnpackListArgumentAST[source]

Bases: CodeAST

AST node for an * unpacking expressions in a CallAST (e.g. the *x in f(*x)).

Attributes are:

itemAST

The argument that must evaluate an iterable.

class ll.ul4c.UnpackDictArgumentAST[source]

Bases: CodeAST

AST node for an ** unpacking expressions in a CallAST (e.g. the **x in f(**x)).

Attributes are:

itemAST

The argument that must evaluate to a dictionary or an iterable of (key, value) pairs.

class ll.ul4c.ListAST[source]

Bases: CodeAST

AST node for creating a list object (e.g. [x, y, *z]).

Attributes are:

itemslist

The items that will be put into the newly created list as a list of SeqItemAST (x and y in the above example) and UnpackSeqItemAST objects (z in the above example).

class ll.ul4c.ListComprehensionAST[source]

Bases: CodeAST

AST node for a list comprehension (e.g. [v for (a, b) in w if c].

Attributes are:

itemAST

The expression for the item in the newly created list (v in the above example).

varnamenested tuple of VarAST objects

The loop variable (or variables) (a and b in the above example).

containerAST

The container or iterable object over which to loop (w in the above example).

conditionAST or None

The condition (as an AST object if there is one, or None if there is not) (c in the above example).

class ll.ul4c.SetAST[source]

Bases: CodeAST

AST node for creating a set object (e.g. {x, y, *z}.

Attributes are:

itemslist

The items that will be put into the newly created set as a list of SeqItemAST (x and y in the above example) and UnpackSeqItemAST objects (z in the above example).

class ll.ul4c.SetComprehensionAST[source]

Bases: CodeAST

AST node for a set comprehension (e.g. {v for (a, b) in w if c}.

Attributes are:

itemAST

The expression for the item in the newly created set (v in the above example).

varnamenested tuple of VarAST objects

The loop variable (or variables) (a and b in the above example).

containerAST

The container or iterable object over which to loop (w in the above example).

conditionAST or None

The condition (as an AST object if there is one, or None if there is not) (c in the above example).

class ll.ul4c.DictAST[source]

Bases: CodeAST

AST node for creating a dict object (e.g. {k: v, **u}.

Attributes are:

itemslist

The items that will be put into the newly created dictionary as a list of DictItemAST (for k and v in the above example) and UnpackDictItemAST objects (for u in the above example).

class ll.ul4c.DictComprehensionAST[source]

Bases: CodeAST

AST node for a dictionary comprehension (e.g. {k: v for (a, b) in w if c}.

Attributes are:

keyAST

The expression for the keys in the newly created dictionary (k in the above example).

valueAST

The expression for the values in the newly created dictionary (v in the above example).

varnamenested tuple of VarAST objects

The loop variable (or variables) (a and b in the above example).

containerAST

The container or iterable object over which to loop (w in the above example).

conditionAST or None

The condition (as an AST object if there is one, or None if there is not) (c in the above example).

class ll.ul4c.GeneratorExpressionAST[source]

Bases: CodeAST

AST node for a generator expression (e.g. (x for (a, b) in w if c)).

Attributes are:

itemAST

An expression for the item that looping over the generator expression will produce (x in the above example).

varnamenested tuple of VarAST objects

The loop variable (or variables) (a and b in the above example).

containerAST

The container or iterable object over which to loop (w in the above example).

conditionAST or None

The condition (as an AST object if there is one, or None if there is not) (c in the above example).

class ll.ul4c.VarAST[source]

Bases: CodeAST

AST node for getting a variable.

Attributes are:

namestr

The name of the variable.

class ll.ul4c.BlockAST[source]

Bases: CodeAST

Base class for all AST nodes that are blocks.

A block contains a sequence of AST nodes that are executed sequencially. A block may execute its content zero (e.g. an <?if?> block) or more times (e.g. a <?for?> block).

Attributes are:

contentlist of AST objects

The content of the block.

class ll.ul4c.ConditionalBlocksAST[source]

Bases: BlockAST

AST node for a conditional <?if?>/<?elif?>/<?else?> block.

Attributes are:

contentlist

The content of the ConditionalBlocksAST block is one IfBlockAST followed by zero or more ElIfBlockASTs followed by zero or one ElseBlockAST.

class ll.ul4c.IfBlockAST[source]

Bases: BlockAST

AST node for an <?if?> block in an <?if?>/<?elif?>/<?else?> block.

Attributes are:

conditionAST

The condition in the <?if?> block.

contentlist of AST objects

The content of the <?if?> block.

class ll.ul4c.ElIfBlockAST[source]

Bases: BlockAST

AST node for an <?elif?> block.

Attributes are:

conditionAST

The condition in the <?elif?> block.

contentlist of AST objects

The content of the <?elif?> block.

class ll.ul4c.ElseBlockAST[source]

Bases: BlockAST

AST node for an <?else?> block.

Attributes are:

contentlist of AST objects

The content of the <?else?> block.

class ll.ul4c.ForBlockAST[source]

Bases: BlockAST

AST node for a <?for?> loop.

For example

<?for (a, b) in w?>
   body
<?end for?>

Attributes are:

varnamenested tuple of VarAST objects

The loop variable (or variables) (a and b in the above example).

containerAST

The container or iterable object over which to loop (w in the above example).

contentlist of AST objects

The loop body (body in the above example).

class ll.ul4c.WhileBlockAST[source]

Bases: BlockAST

AST node for a <?while?> loop.

For example

<?while c?>
   body
<?end for?>

Attributes are:

conditionAST

The condition which must be true to continue executing the loops booy (c in the above example).

contentlist of AST objects

The loop body (body in the above example).

class ll.ul4c.BreakAST[source]

Bases: CodeAST

AST node for a <?break?> tag inside a <?for?> loop.

class ll.ul4c.ContinueAST[source]

Bases: CodeAST

AST node for a <?continue?> tag inside a <?for?> block.

class ll.ul4c.AttrAST[source]

Bases: CodeAST

AST node for an expression that gets or sets an attribute of an object. (e.g. x.y).

Attributes are:

objAST

The object from which to get the attribute (x in the above example);

attrnamestr

The name of the attribute ("y" in the above example).

class ll.ul4c.SliceAST[source]

Bases: CodeAST

AST node for creating a slice object (used in obj[index1:index2]).

Attributes are:

index1AST or None

The start index (index1 in the above example).

index2AST or None

The stop index (index2 in the above example).

index1 and index2 may also be None (for missing slice indices, which default to 0 for the start index and the length of the sequence for the stop index).

class ll.ul4c.UnaryAST[source]

Bases: CodeAST

Base class for all AST nodes implementing unary expressions (i.e. operators with one operand).

Atttributes are:

objAST

The operand of the unary operator.

class ll.ul4c.NotAST[source]

Bases: UnaryAST

AST node for a unary “not” expression (e.g. not x).

Attributes are:

objAST

The operand (x in the above example).

class ll.ul4c.NegAST[source]

Bases: UnaryAST

AST node for a unary negation expression (e.g. -x).

Attributes are:

objAST

The operand (x in the above example).

class ll.ul4c.BitNotAST[source]

Bases: UnaryAST

AST node for a bitwise unary “not” expression that returns its operand with its bits inverted (e.g. ~x).

Attributes are:

objAST

The operand (x in the above example).

class ll.ul4c.PrintAST[source]

Bases: UnaryAST

AST node for a <?print?> tag (e.g. <?print x?>).

Attributes are:

objAST

The object to be printed (x in the above example).

class ll.ul4c.PrintXAST[source]

Bases: UnaryAST

AST node for a <?printx?> tag (e.g. <?printx x?>).

Attributes are:

objAST

The object to be printed (x in the above example).

class ll.ul4c.ReturnAST[source]

Bases: UnaryAST

AST node for a <?return?> tag (e.g. <?return x?>).

Attributes are:

objAST

The operand (x in the above example).

class ll.ul4c.BinaryAST[source]

Bases: CodeAST

Base class for all UL4 AST nodes implementing binary expressions (i.e. operators with two operands).

obj1AST

The first operand.

obj2AST

The second operand.

class ll.ul4c.ItemAST[source]

Bases: BinaryAST

AST node for subscripting expression (e.g. x[y]).

Attributes are:

obj1AST

The container object, which must be a list, string or dict (x in the above example).

obj2AST

The index/key object (y in the above example).

class ll.ul4c.IsAST[source]

Bases: BinaryAST

AST node for a binary is comparison expression (e.g. x is y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.IsNotAST[source]

Bases: BinaryAST

AST node for a binary is not comparison expression (e.g. x is not y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.EQAST[source]

Bases: BinaryAST

AST node for the binary equality comparison (e.g. x == y.

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.NEAST[source]

Bases: BinaryAST

AST node for a binary inequality comparison (e.g. x != y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.LTAST[source]

Bases: BinaryAST

AST node for the binary “less than” comparison (e.g. x < y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.LEAST[source]

Bases: BinaryAST

AST node for the binary “less than or equal” comparison (e.g. x <= y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.GTAST[source]

Bases: BinaryAST

AST node for the binary “greater than” comparison (e.g. x > y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.GEAST[source]

Bases: BinaryAST

AST node for the binary “greater than or equal” comparison (e.g. x >= y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.ContainsAST[source]

Bases: BinaryAST

AST node for the binary containment testing operator (e.g. x in y).

Attributes are:

obj1AST

The item/key object (x in the above example).

obj2AST

The container object (y in the above example).

class ll.ul4c.NotContainsAST[source]

Bases: BinaryAST

AST node for an inverted containment testing expression (e.g. x not in y).

Attributes are:

obj1AST

The item/key object (x in the above example).

obj2AST

The container object (y in the above example).

class ll.ul4c.AddAST[source]

Bases: BinaryAST

AST node for a binary addition expression (e.g. x + y).

Attributes are:

obj1AST

The left summand (x in the above example).

obj2AST

The right summand (y in the above example).

class ll.ul4c.SubAST[source]

Bases: BinaryAST

AST node for the binary subtraction expression (e.g. x - y).

class ll.ul4c.MulAST[source]

Bases: BinaryAST

AST node for the binary multiplication expression (e.g. x * y).

Attributes are:

obj1AST

The left factor (x in the above example).

obj2AST

The right factor (y in the above example).

class ll.ul4c.FloorDivAST[source]

Bases: BinaryAST

AST node for a binary truncating division expression (e.g. x // y).

Attributes are:

obj1AST

The dividend (x in the above example).

obj2AST

The divisor (y in the above example).

class ll.ul4c.TrueDivAST[source]

Bases: BinaryAST

AST node for a binary true division expression (e.g. x / y).

Attributes are:

obj1AST

The dividend (x in the above example).

obj2AST

The divisor (y in the above example).

class ll.ul4c.ModAST[source]

Bases: BinaryAST

AST node for a binary modulo expression (e.g. x % y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.ShiftLeftAST[source]

Bases: BinaryAST

AST node for a bitwise left shift expression (e.g. x << y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.ShiftRightAST[source]

Bases: BinaryAST

AST node for a bitwise right shift expression (e.g. x >> y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.BitAndAST[source]

Bases: BinaryAST

AST node for a binary bitwise “and” expression (e.g x & y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.BitXOrAST[source]

Bases: BinaryAST

AST node for the binary bitwise “exclusive or” expression (e.g. x ^ y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.BitOrAST[source]

Bases: BinaryAST

AST node for a binary bitwise “or” expression (e.g. x | y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.AndAST[source]

Bases: BinaryAST

AST node for the binary “and” expression (i.e. x and y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.OrAST[source]

Bases: BinaryAST

AST node for a binary “or” expression (e.g. x or y).

Attributes are:

obj1AST

The item/key object (x in the above example).

obj2AST

The container object (y in the above example).

class ll.ul4c.IfAST[source]

Bases: CodeAST

AST node for the ternary inline if/else operator (e.g. x if y else z).

Attributes are:

objifAST

The value of the if/else expression when the condition is true (x in the above example).

objcondAST

The condition (y in the above example).

objelseAST

The value of the if/else expression when the condition is false (z in the above example).

class ll.ul4c.ChangeVarAST[source]

Bases: CodeAST

Base class for all AST nodes that are assignment operators, i.e. that set or modify a variable/attribute or item.

Attributes are:

lvalueAST

The left hand side, i.e. the value that will be modified.

valueAST

The right hand side, the value that will be assigned or be used to modify the intial value.

class ll.ul4c.SetVarAST[source]

Bases: ChangeVarAST

AST node for setting a variable, attribute or item to a value (e.g. x = y).

lvalueAST

The left hand side (x in the above example).

valueAST

The right hand side (y in the above example).

class ll.ul4c.AddVarAST[source]

Bases: ChangeVarAST

AST node for an augmented assignment expression that adds a value to a variable (e.g. x += y).

Attributes are:

lvalueAST

The left hand side (x in the above example).

valueAST

The right hand side (y in the above example).

class ll.ul4c.SubVarAST[source]

Bases: ChangeVarAST

AST node for an augmented assignment expression that subtracts a value from a variable/attribute/item. (e.g. x -= y).

Attributes are:

lvalueAST

The left hand side (x in the above example).

valueAST

The right hand side (y in the above example).

class ll.ul4c.MulVarAST[source]

Bases: ChangeVarAST

AST node for an augmented assignment expression that assigns the result of a multiplication to its left operand. (e.g. x *= y).

Attributes are:

lvalueAST

The left hand side (x in the above example).

valueAST

The right hand side (y in the above example).

class ll.ul4c.FloorDivVarAST[source]

Bases: ChangeVarAST

AST node for augmented assignment expression that divides a variable by a value, truncating to an integer value (e.g. x //= y).

Attributes are:

lvalueAST

The left hand side (x in the above example).

valueAST

The right hand side (y in the above example).

class ll.ul4c.TrueDivVarAST[source]

Bases: ChangeVarAST

AST node for an augmented assignment expression that assigns the result of a truncation division to its left operand. (e.g. x //= y).

Attributes are:

lvalueAST

The left hand side (x in the above example).

valueAST

The right hand side (y in the above example).

class ll.ul4c.ModVarAST[source]

Bases: ChangeVarAST

AST node for an augmented assignment expression that assigns the result of a modulo expression to its left operand. (e.g. x %= y).

Attributes are:

lvalueAST

The left hand side (x in the above example).

valueAST

The right hand side (y in the above example).

class ll.ul4c.ShiftLeftVarAST[source]

Bases: ChangeVarAST

AST node for an augmented assignment expression that assigns the result of a “shift left” expression to its left operand. (e.g. x <<= y).

Attributes are:

lvalueAST

The left hand side (x in the above example).

valueAST

The right hand side (y in the above example).

class ll.ul4c.ShiftRightVarAST[source]

Bases: ChangeVarAST

AST node for an augmented assignment expression that assigns the result of a “shift right” expression to its left operand. (e.g. x >>= y).

Attributes are:

lvalueAST

The left hand side (x in the above example).

valueAST

The right hand side (y in the above example).

class ll.ul4c.BitAndVarAST[source]

Bases: ChangeVarAST

AST node for an augmented assignment expression that assigns the result of a binary bitwise “and” expression to its left operand. (e.g. x &= y).

Attributes are:

lvalueAST

The left hand side (x in the above example).

valueAST

The right hand side (y in the above example).

class ll.ul4c.BitXOrVarAST[source]

Bases: ChangeVarAST

AST node for an augmented assignment expression that assigns the result of a binary bitwise “exclusive or” expression to its left operand. (e.g. x ^= y).

Attributes are:

obj1AST

The left operand (x in the above example).

obj2AST

The right operand (y in the above example).

class ll.ul4c.BitOrVarAST[source]

Bases: ChangeVarAST

AST node for an augmented assignment expression that assigns the result of a binary bitwise “or” expression to its left operand. (e.g. x |= y).

Attributes are:

lvalueAST

The left hand side (x in the above example).

valueAST

The right hand side (y in the above example).

class ll.ul4c.CallAST[source]

Bases: CodeAST

AST node for calling an object (e.g. f(x, y)).

Attributes are:

objAST

The object to be called (f in the above example) (or rendered/printed in the subclass RenderAST and its subclasses);

argslist

The arguments to the call as a list of PositionalArgumentAST, KeywordArgumentAST, UnpackListArgumentAST or UnpackDictArgumentAST objects (x and y in the above example).

class ll.ul4c.RenderAST[source]

Bases: CallAST

AST node for rendering a template (e.g. <?render t(x)?>.

For a list of attribute see CallAST.

class ll.ul4c.RenderXAST[source]

Bases: RenderAST

AST node for rendering a template and XML-escaping the output (e.g. <?renderx t(x)?>.

For a list of attribute see CallAST.

class ll.ul4c.RenderOrPrintAST[source]

Bases: RenderAST

AST node for rendering a template or printing an object.

<?render_or_print t(x)?>

is equivalent to

<?if istemplate(t)?>
   <?render t(x)?>
<?else?>
   <?print t?>
<?end if?>

except that even if t is not renderable, all argument in the call will be evaluated.

For a list of attribute see CallAST.

class ll.ul4c.RenderOrPrintXAST[source]

Bases: RenderAST

AST node for rendering a template or printing an object (e.g. <?render_or_printx t(x)?>.

For a list of attribute see CallAST.

class ll.ul4c.RenderXOrPrintAST[source]

Bases: RenderAST

AST node for rendering a template or printing an object (e.g. <?renderx_or_print t(x)?>.

For a list of attribute see CallAST.

class ll.ul4c.RenderXOrPrintXAST[source]

Bases: RenderAST

AST node for rendering a template or printing an object (e.g. <?renderx_or_printx t(x)?>.

For a list of attribute see CallAST.

class ll.ul4c.RenderBlockAST[source]

Bases: RenderAST

AST node for rendering a template via a <?renderblock?> block and passing the content of the block as one additional keyword argument named content.

For example

<?renderblock t(a, b)?>
   content
<?end renderblock?>

Attributes are:

objAST

The object to be rendered (t in the above example);

argslist

The arguments to the call as a list of PositionalArgumentAST, KeywordArgumentAST, UnpackListArgumentAST or UnpackDictArgumentAST objects (a and b in the above example).

contentlist of AST objects

The content of the <?renderblock?> tag (content in the above example) that will be passed as a signatureless template as the keyword argument content to the object.

class ll.ul4c.RenderBlocksAST[source]

Bases: RenderAST

AST node for rendering a template and passing additional arguments via nested variable definitions, e.g.:

<?renderblocks t(a, b)?>
   <?code x = 42?>
   <?def n?>
      ...
   <?end def?>
<?end renderblocks?>

Attributes are:

objAST

The object to be rendered (t in the above example);

argslist

The arguments to the call as a list of PositionalArgumentAST, KeywordArgumentAST, UnpackListArgumentAST or UnpackDictArgumentAST objects (a and b in the above example).

contentlist of AST objects

The content of the <?renderblocks?> tag. These must be AST nodes that define variables (e.g. SetVarAST (the <?code x = 42?> in the above example), or Template (the <?def n?>...<?end def?> in the above example)).

class ll.ul4c.Template[source]

Bases: BlockAST

A UL4 template.

A template object is normally created by passing the template source to the constructor. It can also be loaded from the compiled format via the class methods load() (from a stream) or loads() (from a string).

The compiled format can be generated with the methods dump() (which dumps the format to a stream) or dumps() (which returns a string with the compiled format).

Rendering the template can be done with the methods render() (which is a generator) or renders() (which returns a string).

A Template can also be called as a function (returning the result of the first <?return?> tag encountered). In this case all output of the template will be ignored.

For rendering and calling a template with global variables the following methods are available: render_with_globals(), renders_with_globals() and call_with_globals().

A Template object is itself an AST node. Evaluating it will store a TemplateClosure object for this template under the template’s name in the local variables.

__init__(source=None, name=None, *, namespace=None, whitespace='keep', signature=None)[source]

Create a Template object.

If source is None, the Template remains uninitialized, otherwise source will be compiled.

name is the name of the template. It will be used in exception messages and should be a valid Python identifier.

namespace is the namespace name. It defaults to None.

whitespace specifies how whitespace is handled in the literal text in templates (i.e. the text between the tags):

"keep"

Whitespace is kept as it is.

"strip"

Strip linefeeds and the following indentation from the text. However trailing whitespace at the end of the line will still be honored.

"smart"

If a line contains only indentation and one tag that isn’t a print or printx tag, the indentation and the linefeed after the tag will be stripped from the text. Furthermore the additional indentation that might be introduced by a for, if, elif, else or def block will be ignored. So for example the output of:

<?code langs = ["Python", "Java", "Javascript"]?>
<?if langs?>
   <?for lang in langs?>
      <?print lang?>
   <?end for?>
<?end if?>

will simply be:

Python
Java
Javascript

without any additional empty lines or indentation.

(Output will always be ignored when calling a template as a function.)

signature is the signature of the template. For a top level template it can be:

None

The template will accept all keyword arguments.

An inspect.Signature object

This signature will be used as the signature of the template.

A callable

The signature of the callable will be used.

A string

The signature as a string, i.e. something like "x, y=42, *args, **kwargs". This string will be parsed and evaluated to create the signature for the template.

If the template is a locally defined subtemplate (i.e. a template defined by another template via <?def t?>...<?end def?>), signature can be:

None

The template will accept all arguments.

A SignatureAST object

This AST node will be evaluated at the point of definition of the subtemplate to create the final signature of the subtemplate.

classmethod loads(data)[source]

Loads a template as an UL4ON dump from the string data.

classmethod load(stream)[source]

Loads the template as an UL4ON dump from the stream stream. format.

dump(stream)[source]

Dump the template in compiled UL4ON format to the stream stream.

dumps()[source]

Return the template in compiled UL4ON format (as a string).

render(*args, **kwargs)[source]

Render the template iteratively (i.e. this is a generator).

args and kwargs contain the top level positional and keyword arguments available to the template code. Positional arguments will only be supported if the template has a signature.

render_with_globals(args, kwargs, globals)[source]

Render the template iteratively (i.e. this is a generator).

args and kwargs contain the top level positional and keyword arguments available to the template code. globals contains global variables. Positional arguments will only be supported if the template has a signature.

renders(*args, **kwargs)[source]

Render the template as a string.

args and kwargs contain the top level positional and keyword arguments available to the template code. Positional arguments will only be supported if the template has a signature.

renders_with_globals(args, kwargs, globals)[source]

Render the template as a string.

args and kwargs contain the top level positional and keyword arguments available to the template code. globals contains global variables. Positional arguments will only be supported if the template has a signature.

ul4_call(context, /, *args, **kwargs)[source]

Call the template as a function and return the resulting value.

args and kwargs contain the top level positional and keyword arguments available to the template code. Positional arguments will only be supported if the template has a signature.

__call__(*args, **kwargs)[source]

Call the template as a function and return the resulting value.

args and kwargs contain the top level positional and keyword arguments available to the template code. Positional arguments will only be supported if the template has a signature.

call_with_globals(args, kwargs, globals)[source]

Call the template as a function and return the resulting value.

args and kwargs contain the top level positional and keyword arguments available to the template code. globals contains global variables. Positional arguments will only be supported if the template has a signature.

jssource()[source]

Return the template as the source code of a Javascript function.

javasource()[source]

Return the template as Java source code.

class ll.ul4c.SignatureAST[source]

Bases: CodeAST

AST node for the signature of a locally defined subtemplate.

Attributes are:

paramslist

The parameter. Each list item is a tuple with three items:

namestr

The name of the argument.

typestr

The type of the argument. One of:

  • pk (positional or keyword argument without default)

  • pk= (positional or keyword argument with default)

  • p (positional-only argument without default)

  • p= (positional-only argument with default)

  • k (keyword-only argument without default)

  • k= (keyword-only argument with default)

  • * (argument that collects addition positional arguments)

  • ** (argument that collects addition keyword arguments)

defaultAST or None

The default value for the argument (or None if the argument has no default value).

class ll.ul4c.TemplateClosure[source]

Bases: BlockAST

A locally defined subtemplate.

A TemplateClosure is a closure, i.e. it can use the local variables of the template it is defined in.

class ll.ul4c.BoundTemplate[source]

Bases: object

A template bound to an object.

Calling or rendering a BoundTemplate instance passes the object to which the template is bound as the first positional argument.

render(*args, **kwargs)[source]

Render the template iteratively (i.e. this is a generator).

args and kwargs contain the top level positional and keyword arguments available to the template code. Positional arguments will only be supported if the template has a signature.

render_with_globals(args, kwargs, globals)[source]

Render the template iteratively (i.e. this is a generator).

args and kwargs contain the top level positional and keyword arguments available to the template code. globals contains global variables. Positional arguments will only be supported if the template has a signature.

renders(*args, **kwargs)[source]

Render the template as a string.

args and kwargs contain the top level positional and keyword arguments available to the template code. Positional arguments will only be supported if the template has a signature.

renders_with_globals(args, kwargs, globals)[source]

Render the template as a string.

args and kwargs contain the top level positional and keyword arguments available to the template code. globals contains global variables. Positional arguments will only be supported if the template has a signature.

__call__(*args, **kwargs)[source]

Call the template as a function and return the resulting value.

args and kwargs contain the top level positional and keyword arguments available to the template code. Positional arguments will only be supported if the template has a signature.

call_with_globals(args, kwargs, globals)[source]

Call the template as a function and return the resulting value.

args and kwargs contain the top level positional and keyword arguments available to the template code. globals contains global variables. Positional arguments will only be supported if the template has a signature.