Commits

Rick Copeland committed 2660f1d

Add kajiki as a possible renderer, with tests for dotted loader

  • Participants
  • Parent commits f9701cf

Comments (0)

Files changed (6)

                     'repoze.who-testutil >= 1.0rc3',
                     'wsgiref',
                     'tw.forms',
+                     'Kajiki>=0.2.1',
                     ]
 
 setup(

File tests/test_stack/__init__.py

             values = {}
         AppConfig.__init__(self)
         #First we setup some base values that we know will work
-        self.renderers = ['genshi', 'mako', 'chameleon_genshi', 'jinja','json']
+        self.renderers = ['genshi', 'mako', 'chameleon_genshi', 'jinja','json', 'kajiki']
         self.render_functions = tg.util.Bunch()
         self.package = tests.test_stack
         self.default_renderer = 'genshi'

File tests/test_stack/rendering/controllers/root.py

     def chameleon_index_dotted(self):
         return {}
 
+    @expose('kajiki:tests.test_stack.rendering.templates.index')
+    def kajiki_index_dotted(self):
+        return {}
+
     @expose('genshi:tests.test_stack.rendering.templates.index')
     def index_dotted(self):
         return {}

File tests/test_stack/rendering/test_dotted_rendering.py

     assert "Welcome" in resp, resp
     assert "TurboGears" in resp, resp
 
+def test_default_kajiki_renderer():
+    app = setup_noDB()
+    resp = app.get('/kajiki_index_dotted')
+    assert "Welcome" in resp, resp
+    assert "TurboGears" in resp, resp
+
 def test_default_genshi_renderer():
     app = setup_noDB()
     resp = app.get('/index_dotted')

File tg/configuration.py

 
         self.render_functions.genshi = render_genshi
 
+    def setup_kajiki_renderer(self):
+        """Setup a renderer and loader for the fastpt engine."""
+        from kajiki.loader import PackageLoader
+        from tg.render import render_kajiki
+        loader = PackageLoader()
+        config['pylons.app_globals'].kajiki_loader = loader
+        self.render_functions.kajiki = render_kajiki
+
     def setup_jinja_renderer(self):
         """Setup a renderer and loader for Jinja2 templates."""
         from jinja2 import ChoiceLoader, Environment, FileSystemLoader

File tg/render.py

 from pylons import (app_globals, session, tmpl_context, request,
                     response, templating)
 from repoze.what import predicates
+from webhelpers.html import literal
 
 import tg
 from tg.configuration import Bunch
 
 def render_json(template_name, template_vars, **kwargs):
     return tg.json_encode(template_vars)
+
+def render_kajiki(template_name, extra_vars=None, cache_key=None, 
+                  cache_type=None, cache_expire=None, method='xhtml'):
+    """Render a template with Kajiki
+    
+    Accepts the cache options ``cache_key``, ``cache_type``, and
+    ``cache_expire`` in addition to method which are passed to Kajiki's
+    render function.
+    
+    """
+    # Create a render callable for the cache function
+    def render_template():
+        # Pull in extra vars if needed
+        globs = extra_vars or {}
+        
+        # Second, get the globals
+        globs.update(templating.pylons_globals())
+
+        # Grab a template reference
+        template = globs['app_globals'].kajiki_loader.load(template_name)
+        
+        return literal(template(globs).render())
+    
+    return templating.cached_template(template_name, render_template, cache_key=cache_key,
+                           cache_type=cache_type, cache_expire=cache_expire,
+                           ns_options=('method'), method=method)
+
+