summaryrefslogtreecommitdiff
path: root/pystache/parsed.py
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-10-20 15:57:23 -0700
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-10-20 15:57:23 -0700
commitba5ef6d31eb2705df846444b56893b555948fbf8 (patch)
treeadf04a7e2df34a7442b448573593cf316b3fca88 /pystache/parsed.py
parentb8a3d0c6cea62874bac2414db2c06856293d8877 (diff)
parent5e9a226992ce9b3c9fa1ecd9de700b8d8ff4f136 (diff)
downloadpystache-ba5ef6d31eb2705df846444b56893b555948fbf8.tar.gz
Merge branch 'development' into 'master': staging v0.5.3-rc
Diffstat (limited to 'pystache/parsed.py')
-rw-r--r--pystache/parsed.py48
1 files changed, 23 insertions, 25 deletions
diff --git a/pystache/parsed.py b/pystache/parsed.py
index a37565b..372d96c 100644
--- a/pystache/parsed.py
+++ b/pystache/parsed.py
@@ -3,50 +3,48 @@
"""
Exposes a class that represents a parsed (or compiled) template.
-This module is meant only for internal use.
-
"""
class ParsedTemplate(object):
- def __init__(self, parse_tree):
- """
- Arguments:
+ """
+ Represents a parsed or compiled template.
- parse_tree: a list, each element of which is either--
+ An instance wraps a list of unicode strings and node objects. A node
+ object must have a `render(engine, stack)` method that accepts a
+ RenderEngine instance and a ContextStack instance and returns a unicode
+ string.
- (1) a unicode string, or
- (2) a "rendering" callable that accepts a ContextStack instance
- and returns a unicode string.
+ """
- The possible rendering callables are the return values of the
- following functions:
+ def __init__(self):
+ self._parse_tree = []
- * RenderEngine._make_get_escaped()
- * RenderEngine._make_get_inverse()
- * RenderEngine._make_get_literal()
- * RenderEngine._make_get_partial()
- * RenderEngine._make_get_section()
+ def __repr__(self):
+ return repr(self._parse_tree)
+ def add(self, node):
"""
- self._parse_tree = parse_tree
+ Arguments:
- def __repr__(self):
- return "[%s]" % (", ".join([repr(part) for part in self._parse_tree]))
+ node: a unicode string or node object instance. See the class
+ docstring for information.
+
+ """
+ self._parse_tree.append(node)
- def render(self, context):
+ def render(self, engine, context):
"""
Returns: a string of type unicode.
"""
# We avoid use of the ternary operator for Python 2.4 support.
- def get_unicode(val):
- if callable(val):
- return val(context)
- return val
+ def get_unicode(node):
+ if type(node) is unicode:
+ return node
+ return node.render(engine, context)
parts = map(get_unicode, self._parse_tree)
s = ''.join(parts)
return unicode(s)
-