How to use the jinja.nodes function in Jinja

To help you get started, we’ve selected a few Jinja examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pallets / jinja / jinja / parser.py View on Github external
elif self.stream.current.type == 'lbracket':
            self.stream.next()
            args = []
            while self.stream.current.type != 'rbracket':
                if args:
                    self.stream.expect('comma')
                args.append(self.parse_subscribed_expression())
            self.stream.expect('rbracket')
            if len(args) == 1:
                arg = args[0]
            else:
                arg = nodes.TupleExpression(args, lineno, self.filename)
        else:
            raise TemplateSyntaxError('expected subscript expression',
                                      self.lineno, self.filename)
        return nodes.SubscriptExpression(node, arg, lineno, self.filename)
github pallets / jinja / jinja / parser.py View on Github external
def parse_list_expression(self):
        """
        Parse something like {{ [1, 2, "three"] }}
        """
        token = self.stream.expect('lbracket')
        items = []
        while self.stream.current.type != 'rbracket':
            if items:
                self.stream.expect('comma')
            if self.stream.current.type == 'rbracket':
                break
            items.append(self.parse_expression())
        self.stream.expect('rbracket')

        return nodes.ListExpression(items, token.lineno, self.filename)
github pallets / jinja / jinja / parser.py View on Github external
args.append(None)
        elif self.stream.current.type not in ('rbracket', 'comma'):
            args.append(self.parse_expression())
        else:
            args.append(None)

        if self.stream.current.type == 'colon':
            self.stream.next()
            if self.stream.current.type not in ('rbracket', 'comma'):
                args.append(self.parse_expression())
            else:
                args.append(None)
        else:
            args.append(None)

        return nodes.SliceExpression(*(args + [lineno, self.filename]))
github pallets / jinja / jinja / parser.py View on Github external
"""
        lineno = self.stream.lineno
        filters = []
        while self.stream.current.type == 'pipe':
            self.stream.next()
            token = self.stream.expect('name')
            args = []
            if self.stream.current.type == 'lparen':
                self.stream.next()
                while self.stream.current.type != 'rparen':
                    if args:
                        self.stream.expect('comma')
                    args.append(self.parse_expression())
                self.stream.expect('rparen')
            filters.append((token.value, args))
        return nodes.FilterExpression(node, filters, lineno, self.filename)
github pallets / jinja / jinja / parser.py View on Github external
def parse_undefined_expression(self):
        """
        Parse an undefined literal.
        """
        token = self.stream.expect('name', 'undefined')
        return nodes.UndefinedExpression(token.lineno, self.filename)
github pallets / jinja / jinja / parser.py View on Github external
def assemble_list():
            push_buffer()
            return nodes.NodeList(result, lineno, self.filename)
github pallets / jinja / jinja / parser.py View on Github external
def parse_add_expression(self):
        """
        Parse something like {{ foo + bar }}.
        """
        lineno = self.stream.lineno
        left = self.parse_sub_expression()
        while self.stream.current.type == 'add':
            self.stream.next()
            right = self.parse_sub_expression()
            left = nodes.AddExpression(left, right, lineno, self.filename)
            lineno = self.stream.lineno
        return left
github pallets / jinja / jinja / parser.py View on Github external
else:
            negated = False
        name = self.stream.expect('name').value
        args = []
        if self.stream.current.type == 'lparen':
            self.stream.next()
            while self.stream.current.type != 'rparen':
                if args:
                    self.stream.expect('comma')
                args.append(self.parse_expression())
            self.stream.expect('rparen')
        elif self.stream.current.type in ('name', 'string', 'integer',
                                          'float', 'lparen', 'lbracket',
                                          'lbrace', 'regex'):
            args.append(self.parse_expression())
        node = nodes.TestExpression(node, name, args, token.lineno,
                                    self.filename)
        if negated:
            node = nodes.NotExpression(node, token.lineno, self.filename)
        return node
github pallets / jinja / jinja / parser.py View on Github external
def parse_conditional_expression(self):
        """
        Parse a conditional expression (foo if bar else baz)
        """
        lineno = self.stream.lineno
        expr1 = self.parse_or_expression()
        while self.stream.current.type == 'if':
            self.stream.next()
            expr2 = self.parse_or_expression()
            self.stream.expect('else')
            expr3 = self.parse_conditional_expression()
            expr1 = nodes.ConditionalExpression(expr2, expr1, expr3,
                                                lineno, self.filename)
            lineno = self.stream.lineno
        return expr1