diff options
-rw-r--r-- | HISTORY.rst | 5 | ||||
-rw-r--r-- | pystache/context.py | 21 | ||||
-rw-r--r-- | pystache/renderengine.py | 14 | ||||
-rw-r--r-- | pystache/tests/examples/simple.py | 2 |
4 files changed, 30 insertions, 12 deletions
diff --git a/HISTORY.rst b/HISTORY.rst index 57d97c8..4030f4e 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,6 +1,11 @@ History ======= +0.6.0 (TBD) +----------- + +* Falsey values now coerced to a string using str(). + 0.5.1 (2012-04-03) ----------- 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 '' |