class TestTemplate(PecanTestCase): def setUp(self): super(TestTemplate, self).setUp() self.rf = RendererFactory() def test_available(self): self.assertTrue(self.rf.available('json')) self.assertFalse(self.rf.available('badrenderer')) def test_create_bad(self): self.assertEqual(self.rf.get('doesnotexist', '/'), None) def test_extra_vars(self): extra_vars = self.rf.extra_vars self.assertEqual(extra_vars.make_ns({}), {}) extra_vars.update({'foo': 1}) self.assertEqual(extra_vars.make_ns({}), {'foo': 1}) def test_update_extra_vars(self): extra_vars = self.rf.extra_vars extra_vars.update({'foo': 1}) self.assertEqual(extra_vars.make_ns({'bar': 2}), {'foo': 1, 'bar': 2}) self.assertEqual(extra_vars.make_ns({'foo': 2}), {'foo': 2})
class EmailTemplate(object): """ A helper used to load file-based email templates and generate their text/html and text/plain representations. Can be passed a string template path relative to the app's template root, e.g., templates/emails/signup In this example, EmailTemplate will attempt to find a matching file for: * templates/email/signup.html * templates/email/signup.txt ...for generating both an HTML and plain/text version of the email. """ __html_wrap__ = u''' <html> <body bgcolor="#ffffff" link="#0099cc" alink="#0099cc" vlink="#0099cc" leftmargin="0" topmargin="0" style="padding: 15px; font-family: Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.3em; text-align: left;"> %s </body> </html> '''.strip() def __init__(self, template_file, template_path): self.template_file = template_file self.template_path = template_path self.renderers = RendererFactory() def __render__(self, fmt, ns): template_file = '%s.%s' % (self.template_file, fmt) try: return self.renderers.get('mako', self.template_path).render( template_file, ns ) except exceptions.TopLevelLookupException: pass # This will cause the `__render__` call to return `None` def html(self, ns): body = self.__render__('html', ns) if body: return (self.__html_wrap__ % body.strip()) def text(self, ns): body = self.__render__('txt', ns) if body: return body.strip()
class EmailTemplate(object): """ A helper used to load file-based email templates and generate their text/html and text/plain representations. Can be passed a string template path relative to the app's template root, e.g., templates/emails/signup In this example, EmailTemplate will attempt to find a matching file for: * templates/email/signup.html * templates/email/signup.txt ...for generating both an HTML and plain/text version of the email. """ __html_wrap__ = u''' <html> <body bgcolor="#ffffff" link="#0099cc" alink="#0099cc" vlink="#0099cc" leftmargin="0" topmargin="0" style="padding: 15px; font-family: Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.3em; text-align: left;"> %s </body> </html> '''.strip() def __init__(self, template_file, template_path): self.template_file = template_file self.template_path = template_path self.renderers = RendererFactory() def __render__(self, fmt, ns): template_file = '%s.%s' % (self.template_file, fmt) try: return self.renderers.get('mako', self.template_path).render( template_file, ns) except exceptions.TopLevelLookupException: pass # This will cause the `__render__` call to return `None` def html(self, ns): body = self.__render__('html', ns) if body: return (self.__html_wrap__ % body.strip()) def text(self, ns): body = self.__render__('txt', ns) if body: return body.strip()
class TestTemplate(TestCase): def setUp(self): self.rf = RendererFactory() def test_available(self): self.assertTrue(self.rf.available("json")) self.assertFalse(self.rf.available("badrenderer")) def test_create_bad(self): self.assertEqual(self.rf.get("doesnotexist", "/"), None) def test_extra_vars(self): extra_vars = self.rf.extra_vars self.assertEqual(extra_vars.make_ns({}), {}) extra_vars.update({"foo": 1}) self.assertEqual(extra_vars.make_ns({}), {"foo": 1}) def test_update_extra_vars(self): extra_vars = self.rf.extra_vars extra_vars.update({"foo": 1}) self.assertEqual(extra_vars.make_ns({"bar": 2}), {"foo": 1, "bar": 2}) self.assertEqual(extra_vars.make_ns({"foo": 2}), {"foo": 2})
def setUp(self): self.rf = RendererFactory()
def __init__(self, template_file, template_path): self.template_file = template_file self.template_path = template_path self.renderers = RendererFactory()
def setUp(self): super(TestTemplate, self).setUp() self.rf = RendererFactory()