Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding support for dotted names in inherited templates for Genshi

without removing path based xinclude. This will permit to have extensions
xiinclud'ing they own base templates without hacks in the template loaders
  • Loading branch information...
commit 54e620fb1ac969bb0ed0d15f2943549db65bf71f 1 parent 5a4f06c
@faide faide authored
View
18 .hgignore
@@ -0,0 +1,18 @@
+syntax: glob
+**/*.pyc
+*.swp
+dist/*
+build/*
+lib/*
+TurboGears2.egg-info/*
+zope*
+lxml*
+pytz*
+sourcecodegen*
+kid*
+chameleon*
+SQLAlchemy*
+Jinja*
+TurboKid*
+coverage*
+
View
4 tg/configuration.py
@@ -316,7 +316,7 @@ def setup_genshi_renderer(self):
filter, template loader
"""
- from genshi.template import TemplateLoader
+ from tg.dottednamesupport import GenshiTemplateLoader
from tg.render import render_genshi
def template_loaded(template):
@@ -328,7 +328,7 @@ def template_loaded(template):
"""
template.filters.insert(0, Translator(ugettext))
- loader = TemplateLoader(search_path=self.paths.templates,
+ loader = GenshiTemplateLoader(search_path=self.paths.templates,
auto_reload=self.auto_reload_templates,
callback=template_loaded)
View
22 tg/dottednamesupport.py
@@ -7,6 +7,7 @@
import tg
from mako.template import Template
from paste.deploy.converters import asbool
+from genshi.template import TemplateLoader
try:
import threading
@@ -150,8 +151,8 @@ def __load(self, filename):
def get_template(self, template_name):
"""this is the emulated method that must return a template
instance based on a given template name
-
"""
+
if not self.template_cache.has_key(template_name):
# the template string is not yet loaded into the cache.
# Do so now
@@ -168,3 +169,22 @@ def get_template(self, template_name):
else:
return self.template_cache[template_name]
+class GenshiTemplateLoader(TemplateLoader):
+ """Genshi template loader that supports
+ zipped applications and dotted filenames as well as path names
+ """
+
+ def load(self, filename, relative_to=None, cls=None, encoding=None):
+ """real loader function. copy paste from the mako template
+ loader.
+ """
+ # TODO: get the template extension from the config!!
+ if not filename.endswith('.html'):
+ filename = tg.config['pylons.app_globals'
+ ].dotted_filename_finder.get_dotted_filename(
+ template_name=filename,
+ template_extension='.html')
+
+ return TemplateLoader.load(self, filename,
+ relative_to=relative_to, cls=cls, encoding=encoding)
+
View
16 tg/test_stack/rendering/controllers/root.py
@@ -36,6 +36,14 @@ def process_form_errors(self, **kwargs):
def genshi_inherits(self):
return {}
+ @expose('genshi:genshi_inherits_sub.html')
+ def genshi_inherits_sub(self):
+ return {}
+
+ @expose('genshi:sub/frombottom.html')
+ def genshi_inherits_sub_from_bottom(self):
+ return {}
+
@expose('jinja:jinja_noop.html')
def jinja_index(self):
return {}
@@ -72,6 +80,14 @@ def index_dotted(self):
def genshi_inherits_dotted(self):
return {}
+ @expose('genshi:tg.test_stack.rendering.templates.genshi_inherits_sub_dotted')
+ def genshi_inherits_sub_dotted(self):
+ return {}
+
+ @expose('genshi:tg.test_stack.rendering.templates.sub.frombottom_dotted')
+ def genshi_inherits_sub_dotted_from_bottom(self):
+ return {}
+
@expose('mako:tg.test_stack.rendering.templates.mako_noop')
def mako_index_dotted(self):
return {}
View
15 tg/test_stack/rendering/templates/genshi_inherits_sub.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:py="http://genshi.edgewall.org/"
+ xmlns:xi="http://www.w3.org/2001/XInclude">
+
+ <xi:include href="genshi_master.html" />
+
+<body>
+ <xi:include href="sub/tobeincluded.html" />
+ <div id="getting_started">
+ <p>Inheritance template</p>
+ </div>
+</body>
+</html>
View
15 tg/test_stack/rendering/templates/genshi_inherits_sub_dotted.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:py="http://genshi.edgewall.org/"
+ xmlns:xi="http://www.w3.org/2001/XInclude">
+
+ <xi:include href="genshi_master.html" />
+
+<body>
+ <xi:include href="tg.test_stack.rendering.templates.sub.tobeincluded" />
+ <div id="getting_started">
+ <p>Inheritance template</p>
+ </div>
+</body>
+</html>
View
33 tg/test_stack/rendering/templates/mako_base.mak.py
@@ -0,0 +1,33 @@
+from mako import runtime, filters, cache
+UNDEFINED = runtime.UNDEFINED
+__M_dict_builtin = dict
+__M_locals_builtin = locals
+_magic_number = 5
+_modified_time = 1239703766.6162081
+_template_filename='tg/test_stack/rendering/templates/mako_base.mak'
+_template_uri='/mako_base.mak'
+_template_cache=cache.Cache(__name__, _modified_time)
+_source_encoding='utf-8'
+from webhelpers.html import escape
+_exports = []
+
+
+def render_body(context,**pageargs):
+ context.caller_stack._push_frame()
+ try:
+ __M_locals = __M_dict_builtin(pageargs=pageargs)
+ self = context.get('self', UNDEFINED)
+ __M_writer = context.writer()
+ # SOURCE LINE 2
+ __M_writer(u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html>\n <head>\n ')
+ # SOURCE LINE 6
+ __M_writer(escape(self.head_tags()))
+ __M_writer(u'\n </head>\n <body>\n \t<p>Inside parent template</p>\n ')
+ # SOURCE LINE 10
+ __M_writer(escape(self.body()))
+ __M_writer(u'\n </body>\n</html>\n')
+ return ''
+ finally:
+ context.caller_stack._pop_frame()
+
+
View
34 tg/test_stack/rendering/templates/mako_custom_format.mak.py
@@ -0,0 +1,34 @@
+from mako import runtime, filters, cache
+UNDEFINED = runtime.UNDEFINED
+__M_dict_builtin = dict
+__M_locals_builtin = locals
+_magic_number = 5
+_modified_time = 1239703765.6337719
+_template_filename='tg/test_stack/rendering/templates/mako_custom_format.mak'
+_template_uri='mako_custom_format.mak'
+_template_cache=cache.Cache(__name__, _modified_time)
+_source_encoding='utf-8'
+from webhelpers.html import escape
+_exports = []
+
+
+def render_body(context,**pageargs):
+ context.caller_stack._push_frame()
+ try:
+ __M_locals = __M_dict_builtin(pageargs=pageargs)
+ status = context.get('status', UNDEFINED)
+ format = context.get('format', UNDEFINED)
+ __M_writer = context.writer()
+ # SOURCE LINE 1
+ __M_writer(u'<?xml version="1.0"?>\n<response>\n <status>')
+ # SOURCE LINE 3
+ __M_writer(escape(status))
+ __M_writer(u'</status>\n <format>')
+ # SOURCE LINE 4
+ __M_writer(escape(format))
+ __M_writer(u'</format>\n</response>')
+ return ''
+ finally:
+ context.caller_stack._pop_frame()
+
+
View
50 tg/test_stack/rendering/templates/mako_inherits.mak.py
@@ -0,0 +1,50 @@
+from mako import runtime, filters, cache
+UNDEFINED = runtime.UNDEFINED
+__M_dict_builtin = dict
+__M_locals_builtin = locals
+_magic_number = 5
+_modified_time = 1239703766.6010649
+_template_filename='tg/test_stack/rendering/templates/mako_inherits.mak'
+_template_uri='mako_inherits.mak'
+_template_cache=cache.Cache(__name__, _modified_time)
+_source_encoding='utf-8'
+from webhelpers.html import escape
+_exports = ['head_tags']
+
+
+def _mako_get_namespace(context, name):
+ try:
+ return context.namespaces[(__name__, name)]
+ except KeyError:
+ _mako_generate_namespaces(context)
+ return context.namespaces[(__name__, name)]
+def _mako_generate_namespaces(context):
+ pass
+def _mako_inherit(template, context):
+ _mako_generate_namespaces(context)
+ return runtime._inherit_from(context, '/mako_base.mak', _template_uri)
+def render_body(context,**pageargs):
+ context.caller_stack._push_frame()
+ try:
+ __M_locals = __M_dict_builtin(pageargs=pageargs)
+ __M_writer = context.writer()
+ # SOURCE LINE 2
+ __M_writer(u'\n\n')
+ # SOURCE LINE 6
+ __M_writer(u'\n\n<h1>New Page</h1>\n\n<p>inherited mako page</p>')
+ return ''
+ finally:
+ context.caller_stack._pop_frame()
+
+
+def render_head_tags(context):
+ context.caller_stack._push_frame()
+ try:
+ __M_writer = context.writer()
+ # SOURCE LINE 4
+ __M_writer(u'\n <!-- add some head tags here -->\n')
+ return ''
+ finally:
+ context.caller_stack._pop_frame()
+
+
View
26 tg/test_stack/rendering/templates/mako_noop.mak.py
@@ -0,0 +1,26 @@
+from mako import runtime, filters, cache
+UNDEFINED = runtime.UNDEFINED
+__M_dict_builtin = dict
+__M_locals_builtin = locals
+_magic_number = 5
+_modified_time = 1239703766.5417171
+_template_filename='tg/test_stack/rendering/templates/mako_noop.mak'
+_template_uri='mako_noop.mak'
+_template_cache=cache.Cache(__name__, _modified_time)
+_source_encoding='utf-8'
+from webhelpers.html import escape
+_exports = []
+
+
+def render_body(context,**pageargs):
+ context.caller_stack._push_frame()
+ try:
+ __M_locals = __M_dict_builtin(pageargs=pageargs)
+ __M_writer = context.writer()
+ # SOURCE LINE 2
+ __M_writer(u'\n<p>This is the mako index page</p>')
+ return ''
+ finally:
+ context.caller_stack._pop_frame()
+
+
View
0  tg/test_stack/rendering/templates/sub/__init__.py
No changes.
View
21 tg/test_stack/rendering/templates/sub/frombottom.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:py="http://genshi.edgewall.org/"
+ xmlns:xi="http://www.w3.org/2001/XInclude">
+<xi:include href="../genshi_master.html" />
+<head>
+ <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>
+ <title>Welcome to TurboGears 2.0, standing on the
+ shoulders of giants, since 2007</title>
+</head>
+
+<body>
+ <p>
+ from sub-template: sub.frombottom
+ </p>
+ <div id="getting_started">
+ <p>TurboGears 2 is rapid web application development toolkit designed to make your life easier.</p>
+ </div>
+</body>
+</html>
View
21 tg/test_stack/rendering/templates/sub/frombottom_dotted.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:py="http://genshi.edgewall.org/"
+ xmlns:xi="http://www.w3.org/2001/XInclude">
+<xi:include href="tg.test_stack.rendering.templates.genshi_master" />
+<head>
+ <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>
+ <title>Welcome to TurboGears 2.0, standing on the
+ shoulders of giants, since 2007</title>
+</head>
+
+<body>
+ <p>
+ from sub-template: sub.frombottom_dotted
+ </p>
+ <div id="getting_started">
+ <p>TurboGears 2 is rapid web application development toolkit designed to make your life easier.</p>
+ </div>
+</body>
+</html>
View
3  tg/test_stack/rendering/templates/sub/tobeincluded.html
@@ -0,0 +1,3 @@
+<p>
+ from sub-template: sub.tobeincluded
+</p>
View
13 tg/test_stack/rendering/test_dotted_rendering.py
@@ -34,6 +34,19 @@ def test_genshi_inheritance():
resp = app.get('/genshi_inherits_dotted')
assert "Inheritance template" in resp
assert "Master template" in resp
+
+def test_genshi_sub_inheritance():
+ app = setup_noDB()
+ resp = app.get('/genshi_inherits_sub_dotted')
+ assert "Inheritance template" in resp
+ assert "Master template" in resp
+ assert "from sub-template: sub.tobeincluded" in resp
+
+def test_genshi_sub_inheritance_frombottom():
+ app = setup_noDB()
+ resp = app.get('/genshi_inherits_sub_dotted_from_bottom')
+ assert "Master template" in resp
+ assert "from sub-template: sub.frombottom_dotted" in resp
def test_mako_renderer():
app = setup_noDB()
View
13 tg/test_stack/rendering/test_rendering.py
@@ -27,6 +27,19 @@ def test_genshi_inheritance():
assert "Inheritance template" in resp
assert "Master template" in resp
+def test_genshi_sub_inheritance():
+ app = setup_noDB()
+ resp = app.get('/genshi_inherits_sub')
+ assert "Inheritance template" in resp
+ assert "Master template" in resp
+ assert "from sub-template: sub.tobeincluded" in resp
+
+def test_genshi_sub_inheritance_from_bottom():
+ app = setup_noDB()
+ resp = app.get('/genshi_inherits_sub_from_bottom')
+ assert "from sub-template: sub.frombottom" in resp
+ assert "Master template" in resp
+
def test_chameleon_genshi_base():
app = setup_noDB()
resp = app.get('/chameleon_genshi_index')
Please sign in to comment.
Something went wrong with that request. Please try again.