summaryrefslogtreecommitdiff
path: root/pystache/parsed.py
blob: e94c64485a746b48d64248265610a2960c6c3d3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# coding: utf-8

"""
Exposes a class that represents a parsed (or compiled) template.

"""


class ParsedTemplate(object):

    def __init__(self):
        self._parse_tree = []

    def __repr__(self):
        return repr(self._parse_tree)

    def add(self, node):
        """
        Arguments:

          node: a unicode string or node object instance.  A node object
            instance must have a `render(engine, stack)` method that
            accepts a RenderEngine instance and a ContextStack instance and
            returns a unicode string.

        """
        self._parse_tree.append(node)

    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 type(val) is unicode:
                return val
            return val.render(engine, context)
        parts = map(get_unicode, self._parse_tree)
        s = ''.join(parts)

        return unicode(s)