Skip to content

trestle.core.jinja.base

trestle.core.jinja.base ¤

Trestle core.jinja base class.

Classes¤

TrestleJinjaExtension ¤

Bases: Extension

Class to define common methods to be inherited from for use in trestle.

Source code in trestle/core/jinja/base.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class TrestleJinjaExtension(Extension):
    """Class to define common methods to be inherited from for use in trestle."""

    max_tag_parse = 20

    def __init__(self, environment: Environment) -> None:
        """Ensure enviroment is set and carried into class vars."""
        super().__init__(environment)

    @staticmethod
    def parse_expression(parser):
        """Safely parse jinja expression."""
        # Licensed under MIT from:
        # https://github.com/MoritzS/jinja2-django-tags/blob/master/jdj_tags/extensions.py#L424
        # Due to how the jinja2 parser works, it treats "foo" "bar" as a single
        # string literal as it is the case in python.
        # But the url tag in django supports multiple string arguments, e.g.
        # "{% url 'my_view' 'arg1' 'arg2' %}".
        # That's why we have to check if it's a string literal first.
        token = parser.stream.current
        if token.test(lexer.TOKEN_STRING):
            expr = nodes.Const(token.value, lineno=token.lineno)
            next(parser.stream)
        else:
            expr = parser.parse_expression(False)

        return expr
Attributes¤
max_tag_parse = 20 class-attribute instance-attribute ¤
Functions¤
__init__(environment) ¤

Ensure enviroment is set and carried into class vars.

Source code in trestle/core/jinja/base.py
27
28
29
def __init__(self, environment: Environment) -> None:
    """Ensure enviroment is set and carried into class vars."""
    super().__init__(environment)
parse_expression(parser) staticmethod ¤

Safely parse jinja expression.

Source code in trestle/core/jinja/base.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@staticmethod
def parse_expression(parser):
    """Safely parse jinja expression."""
    # Licensed under MIT from:
    # https://github.com/MoritzS/jinja2-django-tags/blob/master/jdj_tags/extensions.py#L424
    # Due to how the jinja2 parser works, it treats "foo" "bar" as a single
    # string literal as it is the case in python.
    # But the url tag in django supports multiple string arguments, e.g.
    # "{% url 'my_view' 'arg1' 'arg2' %}".
    # That's why we have to check if it's a string literal first.
    token = parser.stream.current
    if token.test(lexer.TOKEN_STRING):
        expr = nodes.Const(token.value, lineno=token.lineno)
        next(parser.stream)
    else:
        expr = parser.parse_expression(False)

    return expr

handler: python