1# coding: utf-8 2 3""" 4Exposes a class that represents a parsed (or compiled) template. 5 6""" 7 8 9class ParsedTemplate(object): 10 11 """ 12 Represents a parsed or compiled template. 13 14 An instance wraps a list of unicode strings and node objects. A node 15 object must have a `render(engine, stack)` method that accepts a 16 RenderEngine instance and a ContextStack instance and returns a unicode 17 string. 18 19 """ 20 21 def __init__(self): 22 self._parse_tree = [] 23 24 def __repr__(self): 25 return repr(self._parse_tree) 26 27 def add(self, node): 28 """ 29 Arguments: 30 31 node: a unicode string or node object instance. See the class 32 docstring for information. 33 34 """ 35 self._parse_tree.append(node) 36 37 def render(self, engine, context): 38 """ 39 Returns: a string of type unicode. 40 41 """ 42 # We avoid use of the ternary operator for Python 2.4 support. 43 def get_unicode(node): 44 if type(node) is str: 45 return node 46 return node.render(engine, context) 47 48 parts = list(map(get_unicode, self._parse_tree)) 49 s = ''.join(parts) 50 51 return str(s) 52