Commit b8b66bf6 by Chris Jerdonek

Merge 'issue_70' into development: recast Simple() example in README.rst.

parents 6fd54a2d 0995ca0d
...@@ -37,16 +37,15 @@ Use It ...@@ -37,16 +37,15 @@ Use It
>>> import pystache >>> import pystache
>>> pystache.render('Hi {{person}}!', {'person': 'Mom'}) >>> pystache.render('Hi {{person}}!', {'person': 'Mom'})
'Hi Mom!' u'Hi Mom!'
You can also create dedicated view classes to hold your view logic. You can also create dedicated view classes to hold your view logic.
Here's your simple.py:: Here's your simple.py::
import pystache >>> class Simple(object):
class Simple(pystache.View): ... def thing(self):
def thing(self): ... return "pizza"
return "pizza"
Then your template, simple.mustache:: Then your template, simple.mustache::
...@@ -54,8 +53,9 @@ Then your template, simple.mustache:: ...@@ -54,8 +53,9 @@ Then your template, simple.mustache::
Pull it together:: Pull it together::
>>> Simple().render() >>> renderer = pystache.Renderer(search_dirs='examples')
'Hi pizza!' >>> renderer.render(Simple())
u'Hi pizza!'
Test It Test It
...@@ -65,7 +65,7 @@ nose_ works great! :: ...@@ -65,7 +65,7 @@ nose_ works great! ::
pip install nose pip install nose
cd pystache cd pystache
nosetests --with-doctest nosetests --with-doctest --doctest-extension=rst
To include tests from the mustache spec_ in your test runs: :: To include tests from the mustache spec_ in your test runs: ::
......
...@@ -48,12 +48,17 @@ class Locator(object): ...@@ -48,12 +48,17 @@ class Locator(object):
""" """
Return the directory containing an object's defining class. Return the directory containing an object's defining class.
Returns None if there is no such directory, for example if the
class was defined in an interactive Python session, or in a
doctest that appears in a text file (rather than a Python file).
""" """
module = sys.modules[obj.__module__] module = sys.modules[obj.__module__]
# TODO: should we handle the case of __file__ not existing, for if not hasattr(module, '__file__'):
# example when using the interpreter or using a module in the # TODO: add a unit test for this case.
# standard library)? return None
path = module.__file__ path = module.__file__
return os.path.dirname(path) return os.path.dirname(path)
......
...@@ -263,11 +263,16 @@ class Renderer(object): ...@@ -263,11 +263,16 @@ class Renderer(object):
class definition. class definition.
""" """
search_dirs = self.search_dirs
locator = self.make_locator() locator = self.make_locator()
template_name = locator.make_template_name(obj) template_name = locator.make_template_name(obj)
directory = locator.get_object_directory(obj) directory = locator.get_object_directory(obj)
search_dirs = [directory] + self.search_dirs # TODO: add a unit test for the case of a None return value.
if directory is not None:
search_dirs = [directory] + self.search_dirs
path = locator.locate_path(template_name=template_name, search_dirs=search_dirs) path = locator.locate_path(template_name=template_name, search_dirs=search_dirs)
return self.read(path) return self.read(path)
...@@ -310,7 +315,7 @@ class Renderer(object): ...@@ -310,7 +315,7 @@ class Renderer(object):
def render(self, template, *context, **kwargs): def render(self, template, *context, **kwargs):
""" """
Render the given template (or templated object) using the given context. Render the given template (or template object) using the given context.
Returns the rendering as a unicode string. Returns the rendering as a unicode string.
...@@ -321,11 +326,11 @@ class Renderer(object): ...@@ -321,11 +326,11 @@ class Renderer(object):
Arguments: Arguments:
template: a template string of type unicode or str, or an object template: a template string of type unicode or str, or an object
instance. If the argument is an object, for the template string instance. If the argument is an object, the function first looks
the function attempts to find a template associated to the for the template associated to the object by calling this class's
object by calling the get_associated_template() method. The get_associated_template() method. The rendering process also
argument in this case is also used as the first element of the uses the passed object as the first element of the context stack
context stack when rendering the associated template. when rendering.
*context: zero or more dictionaries, Context instances, or objects *context: zero or more dictionaries, Context instances, or objects
with which to populate the initial context stack. None with which to populate the initial context stack. None
......
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