diff options
author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-04-25 19:04:45 -0700 |
---|---|---|
committer | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-04-29 13:12:33 -0700 |
commit | 64e2fe30e3d4c4f5804c5e54e5a7d583122268cb (patch) | |
tree | d605ea52f90c006a36493d8dd1b92c90bf7fd7db | |
parent | 29ac86c0dec2f394f80f076ba3e8c996aa65026c (diff) | |
download | pystache-64e2fe30e3d4c4f5804c5e54e5a7d583122268cb.tar.gz |
Started implementing dot notation.
-rw-r--r-- | pystache/context.py | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/pystache/context.py b/pystache/context.py index de22a75..cb53b88 100644 --- a/pystache/context.py +++ b/pystache/context.py @@ -61,22 +61,40 @@ def _get_value(item, key): # TODO: add some unit tests for this. -def resolve(context, name): +def resolve(context_stack, name): """ - Resolve the given name against the given context stack. + Resolve a name against a context stack. This function follows the rules outlined in the section of the spec regarding tag interpolation. + Arguments: + + context_stack: a ContextStack instance. + This function does not coerce the return value to a string. """ if name == '.': - return context.top() + return context_stack.top() + + parts = name.split('.') + + value = context_stack.get(parts[0], _NOT_FOUND) + + for part in parts[1:]: + # TODO: use EAFP here instead. + # http://docs.python.org/glossary.html#term-eafp + if value is _NOT_FOUND: + break + value = _get_value(value, part) + + # The spec says that if name resolution fails at any point, the result + # should be considered falsey, and should interpolate as the empty string. + if value is _NOT_FOUND: + return '' - # 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, '') + return value class ContextStack(object): |