Skip to content

BaseTemplateTag

Sub classes

Name Children Inherits
StandaloneTag
jinjarope.tags
Tag that renders to a single output without content block.
ContainerTag
jinjarope.tags
Tag that wraps content and processes it.

    Base classes

    Name Children Inherits
    Extension
    jinja2.ext
    Extensions can be used to add extra functionality to the Jinja template

    â‹” Inheritance diagram

    graph TD
      97041793788816["tags.BaseTemplateTag"]
      97041787232064["ext.Extension"]
      128674497047040["builtins.object"]
      97041787232064 --> 97041793788816
      128674497047040 --> 97041787232064

    🛈 DocStrings

    Bases: Extension

    Base class for template tag extensions providing common functionality.

    Source code in src/jinjarope/tags.py
    class BaseTemplateTag(Extension):
        """Base class for template tag extensions providing common functionality."""
    
        def __init__(self, environment: Environment) -> None:
            super().__init__(environment)
            self.context: Context | None = None
            self.template: str | None = None
            self.lineno: int | None = None
            self.tag_name: str | None = None
    
        def parse(self, parser: Parser) -> nodes.Node:
            """Parse the tag definition and return a Node."""
            lineno = parser.stream.current.lineno
            tag_name = parser.stream.current.value
            meta_kwargs = [
                nodes.Keyword("_context", nodes.ContextReference()),
                nodes.Keyword("_template", nodes.Const(parser.name)),
                nodes.Keyword("_lineno", nodes.Const(lineno)),
                nodes.Keyword("_tag_name", nodes.Const(tag_name)),
            ]
    
            self.init_parser(parser)
            args, kwargs, options = self.parse_args(parser)
            kwargs.extend(meta_kwargs)
            options["tag_name"] = tag_name
            return self.create_node(parser, args, kwargs, lineno=lineno, **options)
    
        def init_parser(self, parser: Parser) -> None:
            """Initialize parser by skipping the tag name."""
            parser.stream.skip(1)
    
        def parse_args(
            self,
            parser: Parser,
        ) -> tuple[list[nodes.Expr], list[nodes.Keyword], dict[str, Any]]:
            """Parse arguments from the tag definition."""
            args: list[nodes.Expr] = []
            kwargs: list[nodes.Keyword] = []
            options: dict[str, str | None] = {"target": None}
            require_comma = False
            arguments_finished = False
    
            while parser.stream.current.type != "block_end":
                if parser.stream.current.test("name:as"):
                    parser.stream.skip(1)
                    options["target"] = parser.stream.expect("name").value
                    arguments_finished = True
    
                if arguments_finished:
                    if not parser.stream.current.test("block_end"):
                        desc = describe_token(parser.stream.current)
                        parser.fail(
                            f"Expected 'block_end', got {desc!r}",
                            parser.stream.current.lineno,
                        )
                    break
    
                if require_comma:
                    parser.stream.expect("comma")
    
                    # support for trailing comma
                    if parser.stream.current.type == "block_end":
                        break
    
                token = parser.stream.current
                if token.type == "name" and parser.stream.look().type == "assign":
                    key = token.value
                    parser.stream.skip(2)  # Skip name and assign tokens
                    value = parser.parse_expression()
                    kwargs.append(nodes.Keyword(key, value, lineno=value.lineno))
                else:
                    if kwargs:
                        parser.fail("Invalid argument syntax", token.lineno)
                    args.append(parser.parse_expression())
    
                require_comma = True
    
            return args, kwargs, options
    
        def create_node(
            self,
            parser: Parser,
            args: list[nodes.Expr],
            kwargs: list[nodes.Keyword],
            *,
            lineno: int,
            **options: Any,
        ) -> nodes.Node:
            raise NotImplementedError
    

    init_parser

    init_parser(parser: Parser) -> None
    

    Initialize parser by skipping the tag name.

    Source code in src/jinjarope/tags.py
    def init_parser(self, parser: Parser) -> None:
        """Initialize parser by skipping the tag name."""
        parser.stream.skip(1)
    

    parse

    parse(parser: Parser) -> Node
    

    Parse the tag definition and return a Node.

    Source code in src/jinjarope/tags.py
    def parse(self, parser: Parser) -> nodes.Node:
        """Parse the tag definition and return a Node."""
        lineno = parser.stream.current.lineno
        tag_name = parser.stream.current.value
        meta_kwargs = [
            nodes.Keyword("_context", nodes.ContextReference()),
            nodes.Keyword("_template", nodes.Const(parser.name)),
            nodes.Keyword("_lineno", nodes.Const(lineno)),
            nodes.Keyword("_tag_name", nodes.Const(tag_name)),
        ]
    
        self.init_parser(parser)
        args, kwargs, options = self.parse_args(parser)
        kwargs.extend(meta_kwargs)
        options["tag_name"] = tag_name
        return self.create_node(parser, args, kwargs, lineno=lineno, **options)
    

    parse_args

    parse_args(parser: Parser) -> tuple[list[Expr], list[Keyword], dict[str, Any]]
    

    Parse arguments from the tag definition.

    Source code in src/jinjarope/tags.py
    def parse_args(
        self,
        parser: Parser,
    ) -> tuple[list[nodes.Expr], list[nodes.Keyword], dict[str, Any]]:
        """Parse arguments from the tag definition."""
        args: list[nodes.Expr] = []
        kwargs: list[nodes.Keyword] = []
        options: dict[str, str | None] = {"target": None}
        require_comma = False
        arguments_finished = False
    
        while parser.stream.current.type != "block_end":
            if parser.stream.current.test("name:as"):
                parser.stream.skip(1)
                options["target"] = parser.stream.expect("name").value
                arguments_finished = True
    
            if arguments_finished:
                if not parser.stream.current.test("block_end"):
                    desc = describe_token(parser.stream.current)
                    parser.fail(
                        f"Expected 'block_end', got {desc!r}",
                        parser.stream.current.lineno,
                    )
                break
    
            if require_comma:
                parser.stream.expect("comma")
    
                # support for trailing comma
                if parser.stream.current.type == "block_end":
                    break
    
            token = parser.stream.current
            if token.type == "name" and parser.stream.look().type == "assign":
                key = token.value
                parser.stream.skip(2)  # Skip name and assign tokens
                value = parser.parse_expression()
                kwargs.append(nodes.Keyword(key, value, lineno=value.lineno))
            else:
                if kwargs:
                    parser.fail("Invalid argument syntax", token.lineno)
                args.append(parser.parse_expression())
    
            require_comma = True
    
        return args, kwargs, options
    

    :fa-brands-github: Show source on GitHub