Commit 4cf7281e by Chris Jerdonek

Merge 'issue_59' into development: closing issue #59 (Template -> Renderer)

parents b6018af6 a36cd00a
...@@ -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.template import Template from pystache.renderer import Renderer
USAGE = """\ USAGE = """\
...@@ -64,8 +64,10 @@ def main(sys_argv): ...@@ -64,8 +64,10 @@ def main(sys_argv):
except IOError: except IOError:
context = json.loads(context) context = json.loads(context)
template = Template(template) renderer = Renderer()
print(template.render(context))
rendered = renderer.render(template, context)
print rendered
if __name__=='__main__': if __name__=='__main__':
......
...@@ -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 .template 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) renderer = Renderer()
return template.render(context, **kwargs) return renderer.render(template, context, **kwargs)
...@@ -20,22 +20,20 @@ except ImportError: ...@@ -20,22 +20,20 @@ except ImportError:
pass pass
class Template(object): class Renderer(object):
def __init__(self, template=None, load_template=None, output_encoding=None, escape=None, # TODO: change load_template to load_partial.
def __init__(self, load_template=None, output_encoding=None, escape=None,
default_encoding=None, decode_errors='strict'): default_encoding=None, decode_errors='strict'):
""" """
Construct a Template instance. Construct an instance.
Arguments: Arguments:
template: a template string that is either unicode, or of type load_template: a function for loading templates by name, for
str and encoded using the encoding named by the default_encoding example when loading partials. The function should accept a
keyword argument. single template_name parameter and return a template as a string.
Defaults to the default Loader's load_template() method.
load_template: the function for loading partials. The function should
accept a single template_name parameter and return a template as
a string. Defaults to the default Loader's load_template() method.
output_encoding: the encoding to use when rendering to a string. output_encoding: the encoding to use when rendering to a string.
The argument should be the name of an encoding as a string, for The argument should be the name of an encoding as a string, for
...@@ -84,7 +82,6 @@ class Template(object): ...@@ -84,7 +82,6 @@ class Template(object):
self.escape = escape self.escape = escape
self.load_template = load_template self.load_template = load_template
self.output_encoding = output_encoding self.output_encoding = output_encoding
self.template = template
def _unicode_and_escape(self, s): def _unicode_and_escape(self, s):
if not isinstance(s, unicode): if not isinstance(s, unicode):
...@@ -146,9 +143,9 @@ class Template(object): ...@@ -146,9 +143,9 @@ class Template(object):
escape=self._unicode_and_escape) escape=self._unicode_and_escape)
return engine return engine
def render(self, context=None, **kwargs): def render(self, template, context=None, **kwargs):
""" """
Return the template rendered using the given context. Render the given template using the given context.
The return value is a unicode string, unless the output_encoding The return value is a unicode string, unless the output_encoding
attribute has been set to a non-None value, in which case the attribute has been set to a non-None value, in which case the
...@@ -160,6 +157,10 @@ class Template(object): ...@@ -160,6 +157,10 @@ class Template(object):
Arguments: Arguments:
template: a template string that is either unicode, or of type
str and encoded using the encoding named by the default_encoding
keyword argument.
context: a dictionary, Context, or object (e.g. a View instance). context: a dictionary, Context, or object (e.g. a View instance).
**kwargs: additional key values to add to the context when rendering. **kwargs: additional key values to add to the context when rendering.
...@@ -169,13 +170,12 @@ class Template(object): ...@@ -169,13 +170,12 @@ class Template(object):
engine = self._make_render_engine() engine = self._make_render_engine()
context = self._make_context(context, **kwargs) context = self._make_context(context, **kwargs)
template = self.template
if not isinstance(template, unicode): if not isinstance(template, unicode):
template = self.unicode(template) template = self.unicode(template)
result = engine.render(template, context) rendered = engine.render(template, context)
if self.output_encoding is not None: if self.output_encoding is not None:
result = result.encode(self.output_encoding) rendered = rendered.encode(self.output_encoding)
return result return rendered
...@@ -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 .template 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,9 +98,9 @@ class View(object): ...@@ -98,9 +98,9 @@ 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 = self.get_template()
escape=escape) renderer = Renderer(self.load_template, output_encoding=encoding, escape=escape)
return template.render(self.context) return renderer.render(template, self.context)
def get(self, key, default=None): def get(self, key, default=None):
return self.context.get(key, default) return self.context.get(key, default)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import unittest import unittest
import pystache import pystache
from pystache import template from pystache import renderer
class PystacheTests(object): class PystacheTests(object):
...@@ -114,16 +114,16 @@ class PystacheWithoutMarkupsafeTests(PystacheTests, unittest.TestCase): ...@@ -114,16 +114,16 @@ class PystacheWithoutMarkupsafeTests(PystacheTests, unittest.TestCase):
"""Test pystache without markupsafe enabled.""" """Test pystache without markupsafe enabled."""
def setUp(self): def setUp(self):
self.original_markupsafe = template.markupsafe self.original_markupsafe = renderer.markupsafe
template.markupsafe = None renderer.markupsafe = None
def tearDown(self): def tearDown(self):
template.markupsafe = self.original_markupsafe renderer.markupsafe = self.original_markupsafe
# If markupsafe is available, then run the same tests again but without # If markupsafe is available, then run the same tests again but without
# disabling markupsafe. # disabling markupsafe.
_BaseClass = unittest.TestCase if template.markupsafe else object _BaseClass = unittest.TestCase if renderer.markupsafe else object
class PystacheWithMarkupsafeTests(PystacheTests, _BaseClass): class PystacheWithMarkupsafeTests(PystacheTests, _BaseClass):
"""Test pystache with markupsafe enabled.""" """Test pystache with markupsafe enabled."""
...@@ -132,5 +132,5 @@ class PystacheWithMarkupsafeTests(PystacheTests, _BaseClass): ...@@ -132,5 +132,5 @@ class PystacheWithMarkupsafeTests(PystacheTests, _BaseClass):
non_strings_expected = """(123 & ['something'])(chris & 0.9)""" non_strings_expected = """(123 & ['something'])(chris & 0.9)"""
def test_markupsafe_available(self): def test_markupsafe_available(self):
self.assertTrue(template.markupsafe, "markupsafe isn't available. " self.assertTrue(renderer.markupsafe, "markupsafe isn't available. "
"The with-markupsafe tests shouldn't be running.") "The with-markupsafe tests shouldn't be running.")
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,8 @@ class TestSimple(unittest.TestCase): ...@@ -21,7 +21,8 @@ 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") template = '{{#empty_list}}Shouldnt see me {{/empty_list}}{{^empty_list}}Should see me{{/empty_list}}'
self.assertEquals(pystache.Renderer().render(template), "Should see me")
def test_callables(self): def test_callables(self):
view = Lambdas() view = Lambdas()
...@@ -38,8 +39,8 @@ class TestSimple(unittest.TestCase): ...@@ -38,8 +39,8 @@ class TestSimple(unittest.TestCase):
def test_non_existent_value_renders_blank(self): def test_non_existent_value_renders_blank(self):
view = Simple() view = Simple()
template = '{{not_set}} {{blank}}'
self.assertEquals(pystache.Template('{{not_set}} {{blank}}', view).render(), ' ') self.assertEquals(pystache.Renderer().render(template), ' ')
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