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