two similarly-named ``{% block %}`` tags in a template, that template's
parent wouldn't know which one of the blocks' content to use.
+
Super Blocks
============
return wrapped
-def do_xmlattr():
+def do_xmlattr(autospace=False):
"""
Create an SGML/XML attribute string based on the items in a dict.
All values that are neither `none` nor `undefined` are automatically
.. sourcecode:: html+jinja
- <ul {{ {'class': 'my_list', 'missing': None,
+ <ul{{ {'class': 'my_list', 'missing': None,
'id': 'list-%d'|format(variable)}|xmlattr }}>
...
</ul>
...
</ul>
+ As you can see it automatically appends a space in front of the item
+ if the filter returned something. You can disable this by passing
+ `false` as only argument to the filter.
+
*New in Jinja 1.1*
"""
e = escape
e(env.to_unicode(key)),
e(env.to_unicode(value), True)
))
- return u' '.join(result)
+ rv = u' '.join(result)
+ if autospace:
+ rv = ' ' + rv
+ return rv
return wrapped
try:
return iter(seq).next()
except StopIteration:
- return env.undefined_singleton[0]
+ return env.undefined_singleton
return wrapped
try:
return iter(_reversed(seq)).next()
except StopIteration:
- return env.undefined_singleton[-1]
+ return env.undefined_singleton
return wrapped
try:
return choice(seq)
except IndexError:
- return env.undefined_singleton[0]
+ return env.undefined_singleton
return wrapped
todo.extend(node.getChildNodes())
-def get_nodes(nodetype, tree):
+def get_nodes(nodetype, tree, exclude_root=True):
"""
- Get all nodes from nodetype in the tree.
+ Get all nodes from nodetype in the tree excluding the
+ node passed if `exclude_root` is `True` (default).
"""
- todo = [tree]
+ if exclude_root:
+ todo = tree.getChildNodes()
+ else:
+ todo = [tree]
while todo:
node = todo.pop()
if node.__class__ is nodetype:
todo.extend(node.getChildNodes())
+def get_nodes_parentinfo(nodetype, tree):
+ """
+ Like `get_nodes` but it yields tuples in the form ``(parent, node)``.
+ If a node is a direct ancestor of `tree` parent will be `None`.
+
+ Always excludes the root node.
+ """
+ todo = [tree]
+ while todo:
+ node = todo.pop()
+ if node is tree:
+ parent = None
+ else:
+ parent = node
+ for child in node.getChildNodes():
+ if child.__class__ is nodetype:
+ yield parent, child
+ todo.append(child)
+
+
class Node(ast.Node):
"""
Jinja node.
def __repr__(self):
return 'DynamicText(%r, %r)' % (
- self.text,
+ self.format_string,
self.variables
)
return nodes.Trans(flineno, singular, plural, indicator,
replacements or None)
-
def parse_python(self, lineno, gen, template):
"""
Convert the passed generator into a flat string representing
#: the value represents the feature name that appears
#: in the exception.
self.unsupported = {
- ast.ListComp: 'list comprehensions',
- ast.From: 'imports',
- ast.Import: 'imports',
+ ast.ListComp: 'list comprehensions'
}
#: because of python2.3 compatibility add generator
parent = self.environment.loader.parse(node.extends.template,
node.filename)
# look up all block nodes in the current template and
- # add them to the override dict
+ # add them to the override dict.
for n in get_nodes(nodes.Block, node):
overwrites[n.name] = n
# handle direct overrides