def test_relative_include_without_search_path(self): file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w') try: file1.write("""<div>Included</div>""") finally: file1.close() file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w') try: file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include href="tmpl1.html" /> </html>""") finally: file2.close() loader = TemplateLoader() tmpl = loader.load(os.path.join(self.dirname, 'tmpl2.html')) self.assertEqual( """<html> <div>Included</div> </html>""", tmpl.generate().render(encoding=None))
def test_fallback_when_auto_reload_true(self): dirname = tempfile.mkdtemp(suffix='genshi_test') try: file2 = open(os.path.join(dirname, 'tmpl2.html'), 'w') try: file2.write( """<html xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include href="tmpl1.html"><xi:fallback> Missing</xi:fallback></xi:include> </html>""") finally: file2.close() loader = TemplateLoader([dirname], auto_reload=True) tmpl = loader.load('tmpl2.html') self.assertEqual( """<html> Missing </html>""", tmpl.generate().render(encoding=None)) finally: shutil.rmtree(dirname)
def test_include(self): file1 = open(os.path.join(self.dirname, 'tmpl1.txt'), 'wb') try: file1.write("Included\n".encode("utf-8")) finally: file1.close() file2 = open(os.path.join(self.dirname, 'tmpl2.txt'), 'wb') try: file2.write("""----- Included data below this line ----- #include tmpl1.txt ----- Included data above this line -----""".encode("utf-8")) finally: file2.close() loader = TemplateLoader([self.dirname]) tmpl = loader.load('tmpl2.txt', cls=OldTextTemplate) self.assertEqual( """----- Included data below this line ----- Included ----- Included data above this line -----""", tmpl.generate().render(encoding=None))
def test_include_expr(self): file1 = open(os.path.join(self.dirname, 'tmpl1.txt'), 'w') try: file1.write("Included") finally: file1.close() file2 = open(os.path.join(self.dirname, 'tmpl2.txt'), 'w') try: file2.write("""----- Included data below this line ----- {% include ${'%s.txt' % ('tmpl1',)} %} ----- Included data above this line -----""") finally: file2.close() loader = TemplateLoader([self.dirname]) tmpl = loader.load('tmpl2.txt', cls=NewTextTemplate) self.assertEqual( """----- Included data below this line ----- Included ----- Included data above this line -----""", tmpl.generate().render(encoding=None))
def test_include_fallback_with_directive(self): dirname = tempfile.mkdtemp(suffix='genshi_test') try: file2 = open(os.path.join(dirname, 'tmpl2.html'), 'w') try: file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:py="http://genshi.edgewall.org/"> <xi:include href="tmpl1.html"><xi:fallback> <py:if test="True">tmpl1.html not found</py:if> </xi:fallback></xi:include> </html>""") finally: file2.close() loader = TemplateLoader([dirname]) tmpl = loader.load('tmpl2.html') self.assertEqual( """<html> tmpl1.html not found </html>""", tmpl.generate(debug=True).render(encoding=None)) finally: shutil.rmtree(dirname)
def test_include_inline_recursive(self): dirname = tempfile.mkdtemp(suffix='genshi_test') try: file1 = open(os.path.join(dirname, 'tmpl1.html'), 'w') try: file1.write( '<div xmlns:xi="http://www.w3.org/2001/XInclude"' ' xmlns:py="http://genshi.edgewall.org/">' '$depth' '<py:with vars="depth = depth + 1">' '<xi:include href="tmpl1.html"' ' py:if="depth < 3"/>' '</py:with>' '</div>') finally: file1.close() loader = TemplateLoader([dirname], auto_reload=False) tmpl = loader.load(os.path.join(dirname, 'tmpl1.html')) self.assertEqual("<div>0<div>1<div>2</div></div></div>", tmpl.generate(depth=0).render(encoding=None)) finally: shutil.rmtree(dirname)
def test_relative_include_parentdir(self): file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w') try: file1.write("""<div>Included</div>""") finally: file1.close() os.mkdir(os.path.join(self.dirname, 'sub')) file2 = open(os.path.join(self.dirname, 'sub', 'tmpl2.html'), 'w') try: file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include href="../tmpl1.html" /> </html>""") finally: file2.close() loader = TemplateLoader([self.dirname]) tmpl = loader.load('sub/tmpl2.html') self.assertEqual( """<html> <div>Included</div> </html>""", tmpl.generate().render())
def deco(*a, **b): """ Allow to get the docstring back""" try: ## Un-comment bellow for profiling each request: #import cherrypy.lib.profiler as profiler #p = profiler.Profiler("/Users/lia.domide/TVB/profiler/") #return p.run(profile, template_path, func, *a, **b) template_dict = func(*a, **b) if not cfg.RENDER_HTML: return template_dict ### Generate HTML given the path to the template and the data dictionary. loader = TemplateLoader() template = loader.load(template_path) stream = template.generate(**template_dict) return stream.render('xhtml') except Exception, excep: if isinstance(excep, cherrypy.HTTPRedirect): raise excep get_logger("tvb.interface.web.controllers.basecontroller").exception(excep) set_error_message("An unexpected exception appeared. Please contact your system administrator.") raise cherrypy.HTTPRedirect("/tvb?error=True")
def test_absolute_include(self): file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w') try: file1.write("""<div>Included</div>""") finally: file1.close() os.mkdir(os.path.join(self.dirname, 'sub')) file2 = open(os.path.join(self.dirname, 'sub', 'tmpl2.html'), 'w') try: file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include href="%s/tmpl1.html" /> </html>""" % self.dirname) finally: file2.close() loader = TemplateLoader() tmpl = loader.load( os.path.abspath(os.path.join(self.dirname, 'sub', 'tmpl2.html'))) self.assertEqual( """<html> <div>Included</div> </html>""", tmpl.generate().render(encoding=None))
def test_search_path_as_string(self): loader = TemplateLoader(self.dirname) self.assertEqual([self.dirname], loader.search_path)
def test_search_path_empty(self): loader = TemplateLoader() self.assertEqual([], loader.search_path)
def __init__(self, *a, **k): self.loader = TemplateLoader(*a, **k)
import settings from genshi.template.loader import TemplateLoader loader = TemplateLoader(settings.TEMPLATES_DIRS)