Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kajiki import path #121

Open
alexbodn opened this issue Oct 20, 2016 · 0 comments
Open

kajiki import path #121

alexbodn opened this issue Oct 20, 2016 · 0 comments

Comments

@alexbodn
Copy link

alexbodn commented Oct 20, 2016

hello friends,
and a big thank for toscawidgets2?.
i'm porting an older turbogears 2.1.5 app to html5/kajiki, thus tw2 was needed for widgets.
since widgets and regular templates share a few kajiki modules, py:import was needed in tw2 kajiki too.
here is a patch that will get used when invoked from an app that maintains a path for modules and the middleware. turbogears will be patched too.
please commit.

diff --git a/tw2/core/middleware.py b/tw2/core/middleware.py
index 41b1391..bb8b623 100644
--- a/tw2/core/middleware.py
+++ b/tw2/core/middleware.py
@@ -120,7 +120,7 @@ class Config(object):
         'genshi': ['genshi', 'html'],
         'genshi_abs': ['genshi', 'html'], # just for backwards compatibility with tw2 2.0.0
         'jinja':['jinja', 'html'],
-        'kajiki':['kajiki', 'html'],
+        'kajiki':['kajiki', 'html', 'xhtml'],
         'chameleon': ['pt']
     }
     script_name = ''
diff --git a/tw2/core/templating.py b/tw2/core/templating.py
index f6e9c6f..eead876 100644
--- a/tw2/core/templating.py
+++ b/tw2/core/templating.py
@@ -20,7 +20,7 @@ _default_rendering_extension_lookup = {
     'genshi_abs': ['genshi', 'html'],
     'jinja': ['jinja', 'html'],
     'chameleon': ['pt'],
-    'kajiki': ['kajiki', 'html'],
+    'kajiki': ['kajiki', 'html', 'xhtml'],
 }


@@ -122,7 +122,7 @@ def get_source(engine_name, template, inline=False, mw=None):


 @memoize
-def get_render_callable(engine_name, displays_on, src, filename=None, inline=False):
+def get_render_callable(engine_name, displays_on, src, filename=None, inline=False, mw=None):
     """ Returns a function that takes a template source and kwargs. """

     # See the discussion here re: `displays_on` -- http://bit.ly/JRqbRw
@@ -175,8 +175,19 @@ def get_render_callable(engine_name, displays_on, src, filename=None, inline=Fal

     elif engine_name == 'kajiki':
         import kajiki
-        tmpl = kajiki.XMLTemplate(six.u(src), filename=filename,
-                                  cdata_scripts=False)
+        path = getattr(mw.config, 'paths_templates', '') if mw else None
+        if path and not getattr(mw.config, 'kajiki_loader', None):
+            mw.config.kajiki_loader = kajiki.loader.FileLoader(
+                path=path, 
+                reload=getattr(mw.config, 'auto_reload_templates', None),
+                force_mode='xhtml5', 
+                )
+        if path and getattr(mw.config, 'kajiki_loader', None):
+            tmpl = mw.config.kajiki_loader.load(filename, is_fragment=True)
+        else:
+            tmpl = kajiki.XMLTemplate(six.u(src), filename=filename, 
+                                      is_fragment=True, cdata_scripts=False)
+
         return lambda kwargs: Markup(tmpl(kwargs).render())

     elif engine_name == 'chameleon':
@@ -208,7 +219,7 @@ def render(template_name, displays_on, kwargs, inline=False, mw=None):

     # Establish the render function
     callback = get_render_callable(
-        engine_name, displays_on, source, template_name, inline)
+        engine_name, displays_on, source, template_name, inline, mw)

     # Do it

thanks in advance,
alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant