summaryrefslogtreecommitdiff
path: root/pystache
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-04-24 23:14:37 -0700
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-04-24 23:14:37 -0700
commit59a0d80911b4632625007a0959c6637f29228c3f (patch)
tree6188b909bc0f20df72bb437c49177b6348f8eae8 /pystache
parent8d02bb2560f72b35fd64753e8c7ce85a61003f20 (diff)
downloadpystache-59a0d80911b4632625007a0959c6637f29228c3f.tar.gz
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.
Diffstat (limited to 'pystache')
-rw-r--r--pystache/context.py21
-rw-r--r--pystache/renderengine.py14
-rw-r--r--pystache/tests/examples/simple.py2
3 files changed, 25 insertions, 12 deletions
diff --git a/pystache/context.py b/pystache/context.py
index a8f3964..32f1bc4 100644
--- a/pystache/context.py
+++ b/pystache/context.py
@@ -1,7 +1,7 @@
# 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):
"""
diff --git a/pystache/renderengine.py b/pystache/renderengine.py
index d9c822c..f723c32 100644
--- a/pystache/renderengine.py
+++ b/pystache/renderengine.py
@@ -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''
diff --git a/pystache/tests/examples/simple.py b/pystache/tests/examples/simple.py
index 4e611d0..ea82e9d 100644
--- a/pystache/tests/examples/simple.py
+++ b/pystache/tests/examples/simple.py
@@ -12,4 +12,4 @@ class Simple(TemplateSpec):
return "pizza"
def blank(self):
- pass
+ return ''