Haus / haus / components /

""".. _GenshiComponent:

``genshi`` -- Genshi Templating

Render templates using Genshi_. Assumes that
the name of the template is the same as the name
of your handler.

.. _Genshi:


from genshi.template import TemplateLoader, MarkupTemplate, NewTextTemplate

from haus.components.abstract import Component, wraps

class GenshiTemplatingComponent(Component):

    provides = ['render_view']

    def __init__(self, wrk):
        config = wrk.config.get('genshi', {})
        package_text_templates = wrk.pdfilename(
            config.get('text.templates', 'text')
        package_markup_templates = wrk.pdfilename(
            config.get('markup.templates', config.get('templates.dir', 'html'))
        template_search_path = list(config.get('path', []))
        template_search_path.insert(0, package_text_templates)
        template_search_path.insert(0, package_markup_templates)
        self.loader = TemplateLoader(
            auto_reload=config.get('reload', False)
        wrk.functions['render_view'] = self.render_view

    def render_view(self, template_name, stuff, 
                    mode='html', doctype='html', text=False):
        if text:
            klass = NewTextTemplate
            klass = MarkupTemplate
        template = self.loader.load(template_name, cls=klass)
        genned = template.generate(**stuff)
        if text:
            return genned.render()
            return genned.render(mode, doctype=doctype)

    def __call__(self, wrk, *args, **kwargs):
        render = wrk.functions['render_view']
        if kwargs.get('text'):
            text = True
            text = False
        def middleware(app):
            if len(args) == 1:
                template_name = args[0]
                if text:
                    template_name = "%s.txt" % app.__name__
                    template_name = "%s.html" % app.__name__
            def proxy(environ, start_response):
                stuff = app(environ, start_response)
                stuff.setdefault('config', wrk.functions['get_config'](environ))
                if isinstance(stuff, dict):
                    return render(template_name, stuff, text=text)
                    return stuff
            return proxy
        return middleware
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
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.