Source

client_template_demo / template_demo / renderers.py

from pyramid.asset import abspath_from_asset_spec
from mako.lookup import TemplateLookup
import re

class MakoRenderer(object):
    def __init__(self, info):
        package_name = info.package.__name__
        self.lookup = TemplateLookup(
                        directories=abspath_from_asset_spec(
                                "%s:templates" % package_name
                        ))
        self.template_reg = re.compile(
                    r'(?P<fname>[\w_]+)'
                    r'(?:\|(?P<defname>[\w_]+))?'
                    r'(\.(?P<extension>.*))')

    def __call__(self, value, system):
        return self._render(system['renderer_name'], data=value)

    def _render(self, tname, **kw):
        fname, defname, ext = self.template_reg.match(
                                        tname
                                    ).group(
                                        "fname", "defname", "extension"
                                    )
        template = self.lookup.get_template("%s.%s" % (fname, ext))
        if defname:
            template = template.get_def(defname)
        return template.render_unicode(**kw)

class HandlebarsRenderer(MakoRenderer):
    def __call__(self, value, system):
        return super(HandlebarsRenderer, self)._render(
                "handlebars_base.mako", 
                data=value, 
                handlebars=system['renderer_name'])

#class UIRenderer(object):
#
#    def __init__(self, info):
#        self.renderer = zpt_renderer_factory(RendererHelper(
#            'ui.pt', brac2.views, info.registry))
#
#    def __call__(self, value, system):
#        request = system['request']
#        admin_user = request.admin_user
#        resource_types = [
#            {'title': rt.name,
#             'href': request.route_url('resource_type_list', name=rt.name),
#             'api': request.route_url('resource_type_api', name=rt.name),
#             'template': 'resource_type'}
#            for rt in request.dbsession.query(
#                ResourceType).order_by(ResourceType.name)]
#        return self.renderer(
#            {
#             'api': json.dumps(api_links(request)),
#             'data': json.dumps(value, sort_keys=True, indent=4),
#             'template': request.matched_route.name,
#             'resource_types': resource_types,
#             'login': authenticated_userid(request),
#             'adminuser': admin_user.is_admin if admin_user else False,
#            },
#            system)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.