parsed.py 1.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# coding: utf-8

"""
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):
14 15 16 17 18 19
        """
        Arguments:

          parse_tree: a list, each element of which is either--

            (1) a unicode string, or
20
            (2) a "rendering" callable that accepts a ContextStack instance
21 22 23 24 25 26 27 28 29 30 31 32
                and returns a unicode string.

        The possible rendering callables are the return values of the
        following functions:

        * RenderEngine._make_get_escaped()
        * RenderEngine._make_get_inverse()
        * RenderEngine._make_get_literal()
        * RenderEngine._make_get_partial()
        * RenderEngine._make_get_section()

        """
33 34 35 36 37 38 39
        self._parse_tree = parse_tree

    def render(self, context):
        """
        Returns: a string of type unicode.

        """
40 41 42 43 44
        # We avoid use of the ternary operator for Python 2.4 support.
        def get_unicode(val):
            if callable(val):
                return val(context)
            return val
45 46 47 48 49
        parts = map(get_unicode, self._parse_tree)
        s = ''.join(parts)

        return unicode(s)