view roundup/cgi/engine_chameleon.py @ 7867:1774fdf2010a

doc: vale fixes, update TAL repeat object section. For repeat object/variable, added back first() and last() methods now that they are fixed. Moved start/end properties to separate property table along with index. Added details on how even works (e.g. even() is True when number() is 1 because the index is 0 which is even). Document returns from methods are mostly truthy and not true.
author John Rouillard <rouilj@ieee.org>
date Sun, 07 Apr 2024 20:58:58 -0400
parents ac0802452818
children 310e19beba3e
line wrap: on
line source

"""Templating engine adapter for the Chameleon."""

__docformat__ = 'restructuredtext'

import chameleon

from roundup.cgi.templating import context, TALLoaderBase
from roundup.anypy.strings import s2u


class Loader(TALLoaderBase):
    def __init__(self, template_dir):
        self.template_dir = template_dir
        self.loader = chameleon.PageTemplateLoader(template_dir)

    def load(self, tplname):
        src, filename = self._find(tplname)
        return RoundupPageTemplate(self.loader.load(src))


class RoundupPageTemplate(object):
    def __init__(self, pt):
        self._pt = pt

    def render(self, client, classname, request, **options):
        c = context(client, self, classname, request)
        c.update({'options': options})

        def translate(msgid, domain=None, mapping=None, default=None):
            result = client.translator.translate(domain, msgid,
                                                 mapping=mapping,
                                                 default=default)
            return s2u(result)

        output = self._pt.render(None, translate, **c)
        return output.encode(client.charset)

    def __getitem__(self, name):
        return self._pt[name]

    def __getattr__(self, name):
        return getattr(self._pt, name)

Roundup Issue Tracker: http://roundup-tracker.org/