我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用jinja2.nodes.Output()。
def parse(self, parser): stream = parser.stream tag = stream.next() # get arguments args = [] kwargs = [] while not stream.current.test_any('block_end'): if args or kwargs: stream.expect('comma') if stream.current.test('name') and stream.look().test('assign'): key = nodes.Const(stream.next().value) stream.skip() value = parser.parse_expression() kwargs.append(nodes.Pair(key, value, lineno=key.lineno)) else: args.append(parser.parse_expression()) def make_call_node(*kw): return self.call_method('_call', args=[ nodes.List(args), nodes.Dict(kwargs), ], kwargs=kw) return nodes.Output([make_call_node()]).set_lineno(tag.lineno)
def parse(self, parser): lineno = next(parser.stream).lineno # get the first parameter: the view name args = [parser.parse_expression()] # if there's a comma, we've also got an instance variable here if parser.stream.skip_if('comma'): args.append(parser.parse_expression()) else: # no instance supplied for URL tag args.append(nodes.Const(None)) return nodes.Output( [self.call_method('_url', args)], lineno=lineno )
def parse(self, parser): lineno = next(parser.stream).lineno next_token = parser.stream.look() # if there are parameters if next_token.type == "comma": args = [parser.parse_expression()] if parser.stream.skip_if('comma'): args.append(parser.parse_expression()) else: raise TemplateSyntaxError("Missing Lorem Ipsum generator parameter: kind", lineno) if args[1].value not in self.GENERATORS: raise TemplateSyntaxError( "Supported Lorem Ipsum generator kinds are: %s" % ", ".join(self.GENERATORS.keys()), lineno ) else: # if no parameters were supplied args = [nodes.Const(1), nodes.Const("paragraphs")] return nodes.Output( [self.call_method("_lipsum", args)], lineno=lineno )
def parse(self, parser): lineno = next(parser.stream).lineno # get the context context = nodes.ContextReference() # get the arguments args = [context] try: while True: args.append(parser.parse_expression()) except TemplateSyntaxError: pass # no more arguments # get the tag_name for use in looking up callable self.active_tag = parser._tag_stack[-1] # create the node node = self.call_method('_invoke_tag', args=args, lineno=lineno) return nodes.Output( [node], lineno=lineno )
def parse(self, parser): lineno = next(parser.stream).lineno parts = [parser.stream.expect('name').value] while parser.stream.current.type != 'block_end': parser.stream.expect('dot') parts.append(parser.stream.expect('name').value) body = parser.parse_statements(['name:endeditable'], drop_needle=True) call = self.call_method( '_editable_loader', [nodes.Name(parts[-2], 'load'), nodes.Const(parts[-1])]) output_nodes = [nodes.Output([ nodes.MarkSafe(nodes.TemplateData('<div class="editable-container">')), nodes.MarkSafe(nodes.TemplateData('<div class="editable-content">'))]) ] output_nodes.extend(body) output_nodes.extend([ nodes.Output([nodes.MarkSafe(nodes.TemplateData('</div>'))]), nodes.Output([nodes.MarkSafe(call)]), nodes.Output([nodes.MarkSafe(nodes.TemplateData('</div>'))]) ]) block_name = '%s_%s_%d' %(parts[-2], parts[-1], random.randint(0, 500)) return nodes.Block(block_name, output_nodes, True)
def parse(self, parser): lineno = next(parser.stream).lineno parts = [parser.stream.expect('name').value] while parser.stream.current.type != 'block_end': parser.stream.expect('dot') parts.append(parser.stream.expect('name').value) body = parser.parse_statements(['name:endeditable'], drop_needle=True) call = self.call_method( '_editable_loader', [nodes.Name(parts[0], 'load'), nodes.Const(parts[1:]), nodes.Const(not body)]) output_nodes = [ nodes.Output([nodes.MarkSafe(call)]) ] output_nodes.extend(body) output_nodes.extend([ nodes.Output([nodes.MarkSafe(nodes.TemplateData('</span>'))]), ]) block_name = '%s_%s_%d' %(parts[-2], parts[-1], random.randint(0, 500)) return nodes.Block(block_name, output_nodes, True)
def _make_node(self, singular, plural, variables, plural_expr): """Generates a useful node from the data provided.""" # singular only: if plural_expr is None: gettext = nodes.Name('gettext', 'load') node = nodes.Call(gettext, [nodes.Const(singular)], [], None, None) # singular and plural else: ngettext = nodes.Name('ngettext', 'load') node = nodes.Call(ngettext, [ nodes.Const(singular), nodes.Const(plural), plural_expr ], [], None, None) # mark the return value as safe if we are in an # environment with autoescaping turned on if self.environment.autoescape: node = nodes.MarkSafe(node) if variables: node = nodes.Mod(node, variables) return nodes.Output([node])
def parse_print(self): node = nodes.Output(lineno=next(self.stream).lineno) node.nodes = [] while self.stream.current.type != 'block_end': if node.nodes: self.stream.expect('comma') node.nodes.append(self.parse_expression()) return node
def parse(self, parser): stream = parser.stream lineno = stream.next().lineno call_node = self.call_method('render') if stream.next_if('name:as'): var = nodes.Name(stream.expect('name').value, 'store') return nodes.Assign(var, call_node).set_lineno(lineno) else: return nodes.Output([call_node]).set_lineno(lineno)
def parse(self, parser): stream = parser.stream lineno = stream.next().lineno path = parser.parse_expression() call_node = self.call_method('get_statc_url', args=[path]) if stream.next_if('name:as'): var = nodes.Name(stream.expect('name').value, 'store') return nodes.Assign(var, call_node).set_lineno(lineno) else: return nodes.Output([call_node]).set_lineno(lineno)
def parse(self, parser): return nodes.Output([self.call_method('_dump', [ nodes.EnvironmentAttribute('sandboxed'), self.attr('ext_attr'), nodes.ImportedName(__name__ + '.importable_object'), nodes.ContextReference() ])]).set_lineno(next(parser.stream).lineno)
def parse(self, parser): lineno = next(parser.stream).lineno # get the first parameter: the relative URL of the asset file args = [parser.parse_expression()] return nodes.Output( [self.call_method('_asset', args)], lineno=lineno )