summaryrefslogtreecommitdiff
path: root/pystache
diff options
context:
space:
mode:
authorRodrigo Bernardo Pimentel <rbp@isnomore.net>2012-04-03 16:13:53 +0200
committerRodrigo Bernardo Pimentel <rbp@isnomore.net>2012-04-03 16:13:53 +0200
commitc9a4db2bf4e5f1754c1000ddad062e0267ac2a46 (patch)
tree860ef8cd4248beb6293965ebd1242db73c928e6e /pystache
parent599959265ca31b27e407724c192c8bf65cdd4704 (diff)
downloadpystache-c9a4db2bf4e5f1754c1000ddad062e0267ac2a46.tar.gz
Dot notation support, thereby making pystache compliant with Mustache spec v1.1.2 (and updating the spec submodule accordingly)
Diffstat (limited to 'pystache')
-rw-r--r--pystache/context.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/pystache/context.py b/pystache/context.py
index 1621d61..6e75c07 100644
--- a/pystache/context.py
+++ b/pystache/context.py
@@ -27,13 +27,17 @@ def _get_value(item, key):
The Context.get() docstring documents this function's intended behavior.
"""
+ parts = key.split('.')
+ key = parts[0]
+ value = _NOT_FOUND
+
if isinstance(item, dict):
# Then we consider the argument a "hash" for the purposes of the spec.
#
# We do a membership test to avoid using exceptions for flow control
# (e.g. catching KeyError).
if key in item:
- return item[key]
+ value = item[key]
elif type(item).__module__ != '__builtin__':
# Then we consider the argument an "object" for the purposes of
# the spec.
@@ -45,10 +49,15 @@ def _get_value(item, key):
if hasattr(item, key):
attr = getattr(item, key)
if _is_callable(attr):
- return attr()
- return attr
+ value = attr()
+ else:
+ value = attr
+
+ for part in parts[1:]:
+ if value is not _NOT_FOUND:
+ value = _get_value(value, part)
- return _NOT_FOUND
+ return value
class Context(object):