Commit 59a0d809 by Chris Jerdonek

Refactored by adding a resolve() function to context.py:

The resolve() function is responsible for the interpolation name
resolution rules described in the Mustache spec.  Also, falsey values
are now coerced to strings using Python's str(), which is more in line
with the spec.
parent 8d02bb25
History
=======
0.6.0 (TBD)
-----------
* Falsey values now coerced to a string using str().
0.5.1 (2012-04-03)
-----------
......
# coding: utf-8
"""
Defines a Context class to represent mustache(5)'s notion of context.
Exposes a context class and functions to retrieve names from context.
"""
......@@ -60,6 +60,25 @@ def _get_value(item, key):
return _NOT_FOUND
# TODO: add some unit tests for this.
def resolve(context, name):
"""
Resolve the given name against the given context stack.
This function follows the rules outlined in the section of the spec
regarding tag interpolation.
This function does not coerce the return value to a string.
"""
if name == '.':
return context.top()
# The spec says that if the name fails resolution, the result should be
# considered falsey, and should interpolate as the empty string.
return context.get(name, '')
class Context(object):
"""
......
......@@ -7,6 +7,7 @@ Defines a class responsible for rendering logic.
import re
from pystache.context import resolve
from pystache.parser import Parser
......@@ -68,16 +69,7 @@ class RenderEngine(object):
Get a value from the given context as a basestring instance.
"""
val = context.get(tag_name)
# We use "==" rather than "is" to compare integers, as using "is"
# relies on an implementation detail of CPython. The test about
# rendering zeroes failed while using PyPy when using "is".
# See issue #34: https://github.com/defunkt/pystache/issues/34
if not val and val != 0:
if tag_name != '.':
return ''
val = context.top()
val = resolve(context, tag_name)
if callable(val):
# According to the spec:
......@@ -142,6 +134,8 @@ class RenderEngine(object):
Returns a string with type unicode.
"""
# TODO: is there a bug because we are not using the same
# logic as in _get_string_value()?
data = context.get(name)
if data:
return u''
......
......@@ -12,4 +12,4 @@ class Simple(TemplateSpec):
return "pizza"
def blank(self):
pass
return ''
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