Commit d4125da5 by Chris Jerdonek

Renamed Template class to Renderer.

parent 65e347a9
...@@ -20,7 +20,7 @@ import sys ...@@ -20,7 +20,7 @@ import sys
# ValueError: Attempted relative import in non-package # ValueError: Attempted relative import in non-package
# #
from pystache.loader import Loader from pystache.loader import Loader
from pystache.renderer import Template from pystache.renderer import Renderer
USAGE = """\ USAGE = """\
...@@ -64,7 +64,7 @@ def main(sys_argv): ...@@ -64,7 +64,7 @@ def main(sys_argv):
except IOError: except IOError:
context = json.loads(context) context = json.loads(context)
template = Template(template) template = Renderer(template)
print(template.render(context)) print(template.render(context))
......
...@@ -5,12 +5,12 @@ This module contains the initialization logic called by __init__.py. ...@@ -5,12 +5,12 @@ This module contains the initialization logic called by __init__.py.
""" """
from .renderer import Template from .renderer import Renderer
from .view import View from .view import View
from .loader import Loader from .loader import Loader
__all__ = ['Template', 'View', 'Loader', 'render'] __all__ = ['render', 'Loader', 'Renderer', 'View']
def render(template, context=None, **kwargs): def render(template, context=None, **kwargs):
...@@ -18,5 +18,5 @@ def render(template, context=None, **kwargs): ...@@ -18,5 +18,5 @@ def render(template, context=None, **kwargs):
Return the given template string rendered using the given context. Return the given template string rendered using the given context.
""" """
template = Template(template) template = Renderer(template)
return template.render(context, **kwargs) return template.render(context, **kwargs)
...@@ -20,7 +20,7 @@ except ImportError: ...@@ -20,7 +20,7 @@ except ImportError:
pass pass
class Template(object): class Renderer(object):
def __init__(self, template=None, load_template=None, output_encoding=None, escape=None, def __init__(self, template=None, load_template=None, output_encoding=None, escape=None,
default_encoding=None, decode_errors='strict'): default_encoding=None, decode_errors='strict'):
......
...@@ -10,7 +10,7 @@ from types import UnboundMethodType ...@@ -10,7 +10,7 @@ from types import UnboundMethodType
from .context import Context from .context import Context
from .loader import Loader from .loader import Loader
from .renderer import Template from .renderer import Renderer
class View(object): class View(object):
...@@ -68,7 +68,7 @@ class View(object): ...@@ -68,7 +68,7 @@ class View(object):
def _get_template_name(self): def _get_template_name(self):
""" """
Return the name of this Template instance. Return the name of the template to load.
If the template_name attribute is not set, then this method constructs If the template_name attribute is not set, then this method constructs
the template name from the class name as follows, for example: the template name from the class name as follows, for example:
...@@ -98,7 +98,7 @@ class View(object): ...@@ -98,7 +98,7 @@ class View(object):
Return the view rendered using the current context. Return the view rendered using the current context.
""" """
template = Template(self.get_template(), self.load_template, output_encoding=encoding, template = Renderer(self.get_template(), self.load_template, output_encoding=encoding,
escape=escape) escape=escape)
return template.render(self.context) return template.render(self.context)
......
...@@ -10,12 +10,12 @@ import sys ...@@ -10,12 +10,12 @@ import sys
import unittest import unittest
from pystache import renderer from pystache import renderer
from pystache.renderer import Template from pystache.renderer import Renderer
class TemplateTestCase(unittest.TestCase): class RendererTestCase(unittest.TestCase):
"""Test the Template class.""" """Test the Renderer class."""
def setUp(self): def setUp(self):
""" """
...@@ -52,7 +52,7 @@ class TemplateTestCase(unittest.TestCase): ...@@ -52,7 +52,7 @@ class TemplateTestCase(unittest.TestCase):
self.assertEquals(bool(markupsafe), self._was_markupsafe_imported()) self.assertEquals(bool(markupsafe), self._was_markupsafe_imported())
def test_init__escape__default_without_markupsafe(self): def test_init__escape__default_without_markupsafe(self):
template = Template() template = Renderer()
self.assertEquals(template.escape(">'"), ">'") self.assertEquals(template.escape(">'"), ">'")
def test_init__escape__default_with_markupsafe(self): def test_init__escape__default_with_markupsafe(self):
...@@ -61,12 +61,12 @@ class TemplateTestCase(unittest.TestCase): ...@@ -61,12 +61,12 @@ class TemplateTestCase(unittest.TestCase):
return return
self._restore_markupsafe() self._restore_markupsafe()
template = Template() template = Renderer()
self.assertEquals(template.escape(">'"), ">'") self.assertEquals(template.escape(">'"), ">'")
def test_init__escape(self): def test_init__escape(self):
escape = lambda s: "foo" + s escape = lambda s: "foo" + s
template = Template(escape=escape) template = Renderer(escape=escape)
self.assertEquals(template.escape("bar"), "foobar") self.assertEquals(template.escape("bar"), "foobar")
def test_init__default_encoding__default(self): def test_init__default_encoding__default(self):
...@@ -74,7 +74,7 @@ class TemplateTestCase(unittest.TestCase): ...@@ -74,7 +74,7 @@ class TemplateTestCase(unittest.TestCase):
Check the default value. Check the default value.
""" """
template = Template() template = Renderer()
self.assertEquals(template.default_encoding, sys.getdefaultencoding()) self.assertEquals(template.default_encoding, sys.getdefaultencoding())
def test_init__default_encoding(self): def test_init__default_encoding(self):
...@@ -82,7 +82,7 @@ class TemplateTestCase(unittest.TestCase): ...@@ -82,7 +82,7 @@ class TemplateTestCase(unittest.TestCase):
Check that the constructor sets the attribute correctly. Check that the constructor sets the attribute correctly.
""" """
template = Template(default_encoding="foo") template = Renderer(default_encoding="foo")
self.assertEquals(template.default_encoding, "foo") self.assertEquals(template.default_encoding, "foo")
def test_init__decode_errors__default(self): def test_init__decode_errors__default(self):
...@@ -90,7 +90,7 @@ class TemplateTestCase(unittest.TestCase): ...@@ -90,7 +90,7 @@ class TemplateTestCase(unittest.TestCase):
Check the default value. Check the default value.
""" """
template = Template() template = Renderer()
self.assertEquals(template.decode_errors, 'strict') self.assertEquals(template.decode_errors, 'strict')
def test_init__decode_errors(self): def test_init__decode_errors(self):
...@@ -98,17 +98,17 @@ class TemplateTestCase(unittest.TestCase): ...@@ -98,17 +98,17 @@ class TemplateTestCase(unittest.TestCase):
Check that the constructor sets the attribute correctly. Check that the constructor sets the attribute correctly.
""" """
template = Template(decode_errors="foo") template = Renderer(decode_errors="foo")
self.assertEquals(template.decode_errors, "foo") self.assertEquals(template.decode_errors, "foo")
def test_unicode(self): def test_unicode(self):
template = Template() template = Renderer()
actual = template.literal("abc") actual = template.literal("abc")
self.assertEquals(actual, "abc") self.assertEquals(actual, "abc")
self.assertEquals(type(actual), unicode) self.assertEquals(type(actual), unicode)
def test_unicode__default_encoding(self): def test_unicode__default_encoding(self):
template = Template() template = Renderer()
s = "é" s = "é"
template.default_encoding = "ascii" template.default_encoding = "ascii"
...@@ -118,7 +118,7 @@ class TemplateTestCase(unittest.TestCase): ...@@ -118,7 +118,7 @@ class TemplateTestCase(unittest.TestCase):
self.assertEquals(template.unicode(s), u"é") self.assertEquals(template.unicode(s), u"é")
def test_unicode__decode_errors(self): def test_unicode__decode_errors(self):
template = Template() template = Renderer()
s = "é" s = "é"
template.default_encoding = "ascii" template.default_encoding = "ascii"
...@@ -135,38 +135,38 @@ class TemplateTestCase(unittest.TestCase): ...@@ -135,38 +135,38 @@ class TemplateTestCase(unittest.TestCase):
return return
self._restore_markupsafe() self._restore_markupsafe()
_template = Template() template = Renderer()
_template.default_encoding = "utf_8" template.default_encoding = "utf_8"
# Check the standard case. # Check the standard case.
actual = _template.literal("abc") actual = template.literal("abc")
self.assertEquals(actual, "abc") self.assertEquals(actual, "abc")
self.assertEquals(type(actual), renderer.markupsafe.Markup) self.assertEquals(type(actual), renderer.markupsafe.Markup)
s = "é" s = "é"
# Check that markupsafe respects default_encoding. # Check that markupsafe respects default_encoding.
self.assertEquals(_template.literal(s), u"é") self.assertEquals(template.literal(s), u"é")
_template.default_encoding = "ascii" template.default_encoding = "ascii"
self.assertRaises(UnicodeDecodeError, _template.literal, s) self.assertRaises(UnicodeDecodeError, template.literal, s)
# Check that markupsafe respects decode_errors. # Check that markupsafe respects decode_errors.
_template.decode_errors = "replace" template.decode_errors = "replace"
self.assertEquals(_template.literal(s), u'\ufffd\ufffd') self.assertEquals(template.literal(s), u'\ufffd\ufffd')
def test_render__unicode(self): def test_render__unicode(self):
template = Template(u'foo') template = Renderer(u'foo')
actual = template.render() actual = template.render()
self.assertTrue(isinstance(actual, unicode)) self.assertTrue(isinstance(actual, unicode))
self.assertEquals(actual, u'foo') self.assertEquals(actual, u'foo')
def test_render__str(self): def test_render__str(self):
template = Template('foo') template = Renderer('foo')
actual = template.render() actual = template.render()
self.assertTrue(isinstance(actual, unicode)) self.assertTrue(isinstance(actual, unicode))
self.assertEquals(actual, 'foo') self.assertEquals(actual, 'foo')
def test_render__non_ascii_character(self): def test_render__non_ascii_character(self):
template = Template(u'Poincaré') template = Renderer(u'Poincaré')
actual = template.render() actual = template.render()
self.assertTrue(isinstance(actual, unicode)) self.assertTrue(isinstance(actual, unicode))
self.assertEquals(actual, u'Poincaré') self.assertEquals(actual, u'Poincaré')
...@@ -176,7 +176,7 @@ class TemplateTestCase(unittest.TestCase): ...@@ -176,7 +176,7 @@ class TemplateTestCase(unittest.TestCase):
Test render(): passing a context. Test render(): passing a context.
""" """
template = Template('Hi {{person}}') template = Renderer('Hi {{person}}')
self.assertEquals(template.render({'person': 'Mom'}), 'Hi Mom') self.assertEquals(template.render({'person': 'Mom'}), 'Hi Mom')
def test_render__context_and_kwargs(self): def test_render__context_and_kwargs(self):
...@@ -184,7 +184,7 @@ class TemplateTestCase(unittest.TestCase): ...@@ -184,7 +184,7 @@ class TemplateTestCase(unittest.TestCase):
Test render(): passing a context and **kwargs. Test render(): passing a context and **kwargs.
""" """
template = Template('Hi {{person1}} and {{person2}}') template = Renderer('Hi {{person1}} and {{person2}}')
self.assertEquals(template.render({'person1': 'Mom'}, person2='Dad'), 'Hi Mom and Dad') self.assertEquals(template.render({'person1': 'Mom'}, person2='Dad'), 'Hi Mom and Dad')
def test_render__kwargs_and_no_context(self): def test_render__kwargs_and_no_context(self):
...@@ -192,7 +192,7 @@ class TemplateTestCase(unittest.TestCase): ...@@ -192,7 +192,7 @@ class TemplateTestCase(unittest.TestCase):
Test render(): passing **kwargs and no context. Test render(): passing **kwargs and no context.
""" """
template = Template('Hi {{person}}') template = Renderer('Hi {{person}}')
self.assertEquals(template.render(person='Mom'), 'Hi Mom') self.assertEquals(template.render(person='Mom'), 'Hi Mom')
def test_render__context_and_kwargs__precedence(self): def test_render__context_and_kwargs__precedence(self):
...@@ -200,7 +200,7 @@ class TemplateTestCase(unittest.TestCase): ...@@ -200,7 +200,7 @@ class TemplateTestCase(unittest.TestCase):
Test render(): **kwargs takes precedence over context. Test render(): **kwargs takes precedence over context.
""" """
template = Template('Hi {{person}}') template = Renderer('Hi {{person}}')
self.assertEquals(template.render({'person': 'Mom'}, person='Dad'), 'Hi Dad') self.assertEquals(template.render({'person': 'Mom'}, person='Dad'), 'Hi Dad')
def test_render__kwargs_does_not_modify_context(self): def test_render__kwargs_does_not_modify_context(self):
...@@ -209,12 +209,12 @@ class TemplateTestCase(unittest.TestCase): ...@@ -209,12 +209,12 @@ class TemplateTestCase(unittest.TestCase):
""" """
context = {} context = {}
template = Template('Hi {{person}}') template = Renderer('Hi {{person}}')
template.render(context=context, foo="bar") template.render(context=context, foo="bar")
self.assertEquals(context, {}) self.assertEquals(context, {})
def test_render__output_encoding(self): def test_render__output_encoding(self):
template = Template(u'Poincaré') template = Renderer(u'Poincaré')
template.output_encoding = 'utf-8' template.output_encoding = 'utf-8'
actual = template.render() actual = template.render()
self.assertTrue(isinstance(actual, str)) self.assertTrue(isinstance(actual, str))
...@@ -225,7 +225,7 @@ class TemplateTestCase(unittest.TestCase): ...@@ -225,7 +225,7 @@ class TemplateTestCase(unittest.TestCase):
Test passing a non-unicode template with non-ascii characters. Test passing a non-unicode template with non-ascii characters.
""" """
template = Template("déf", output_encoding="utf-8") template = Renderer("déf", output_encoding="utf-8")
# Check that decode_errors and default_encoding are both respected. # Check that decode_errors and default_encoding are both respected.
template.decode_errors = 'ignore' template.decode_errors = 'ignore'
...@@ -235,16 +235,16 @@ class TemplateTestCase(unittest.TestCase): ...@@ -235,16 +235,16 @@ class TemplateTestCase(unittest.TestCase):
template.default_encoding = 'utf_8' template.default_encoding = 'utf_8'
self.assertEquals(template.render(), "déf") self.assertEquals(template.render(), "déf")
# By testing that Template.render() constructs the RenderEngine instance # By testing that Renderer.render() constructs the RenderEngine instance
# correctly, we no longer need to test the rendering code paths through # correctly, we no longer need to test the rendering code paths through
# the Template. We can test rendering paths through only the RenderEngine # the Renderer. We can test rendering paths through only the RenderEngine
# for the same amount of code coverage. # for the same amount of code coverage.
def test_make_render_engine__load_template(self): def test_make_render_engine__load_template(self):
""" """
Test that _make_render_engine() passes the right load_template. Test that _make_render_engine() passes the right load_template.
""" """
template = Template() template = Renderer()
template.load_template = "foo" # in real life, this would be a function. template.load_template = "foo" # in real life, this would be a function.
engine = template._make_render_engine() engine = template._make_render_engine()
...@@ -255,7 +255,7 @@ class TemplateTestCase(unittest.TestCase): ...@@ -255,7 +255,7 @@ class TemplateTestCase(unittest.TestCase):
Test that _make_render_engine() passes the right literal. Test that _make_render_engine() passes the right literal.
""" """
template = Template() template = Renderer()
template.literal = "foo" # in real life, this would be a function. template.literal = "foo" # in real life, this would be a function.
engine = template._make_render_engine() engine = template._make_render_engine()
...@@ -266,7 +266,7 @@ class TemplateTestCase(unittest.TestCase): ...@@ -266,7 +266,7 @@ class TemplateTestCase(unittest.TestCase):
Test that _make_render_engine() passes the right escape. Test that _make_render_engine() passes the right escape.
""" """
template = Template() template = Renderer()
template.unicode = lambda s: s.upper() # a test version. template.unicode = lambda s: s.upper() # a test version.
template.escape = lambda s: "**" + s # a test version. template.escape = lambda s: "**" + s # a test version.
......
import unittest import unittest
import pystache import pystache
from pystache import Template from pystache import Renderer
from examples.nested_context import NestedContext from examples.nested_context import NestedContext
from examples.complex_view import ComplexView from examples.complex_view import ComplexView
from examples.lambdas import Lambdas from examples.lambdas import Lambdas
...@@ -21,7 +21,7 @@ class TestSimple(unittest.TestCase): ...@@ -21,7 +21,7 @@ class TestSimple(unittest.TestCase):
def test_empty_context(self): def test_empty_context(self):
view = ComplexView() view = ComplexView()
self.assertEquals(pystache.Template('{{#empty_list}}Shouldnt see me {{/empty_list}}{{^empty_list}}Should see me{{/empty_list}}', view).render(), "Should see me") self.assertEquals(pystache.Renderer('{{#empty_list}}Shouldnt see me {{/empty_list}}{{^empty_list}}Should see me{{/empty_list}}', view).render(), "Should see me")
def test_callables(self): def test_callables(self):
view = Lambdas() view = Lambdas()
...@@ -39,7 +39,7 @@ class TestSimple(unittest.TestCase): ...@@ -39,7 +39,7 @@ class TestSimple(unittest.TestCase):
def test_non_existent_value_renders_blank(self): def test_non_existent_value_renders_blank(self):
view = Simple() view = Simple()
self.assertEquals(pystache.Template('{{not_set}} {{blank}}', view).render(), ' ') self.assertEquals(pystache.Renderer('{{not_set}} {{blank}}', view).render(), ' ')
def test_template_partial_extension(self): def test_template_partial_extension(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment