diff options
author | kalman@chromium.org <kalman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-08 01:25:17 +0000 |
---|---|---|
committer | kalman@chromium.org <kalman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-08 01:25:17 +0000 |
commit | 61a8ac2d7473df95269d3d4187c040deb0c62d7c (patch) | |
tree | 7c006ee7d82e674dfa5b50abac0171ae1e23a0fc /third_party/handlebar | |
parent | b981988eaf2e4cc0dc465d893d16c50caf6e89fd (diff) | |
download | chromium_src-61a8ac2d7473df95269d3d4187c040deb0c62d7c.zip chromium_src-61a8ac2d7473df95269d3d4187c040deb0c62d7c.tar.gz chromium_src-61a8ac2d7473df95269d3d4187c040deb0c62d7c.tar.bz2 |
Update third_party/handlebar/handlebar.py.
R=cduvall@chromium.org
Review URL: https://chromiumcodereview.appspot.com/10535071
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@141147 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party/handlebar')
-rw-r--r-- | third_party/handlebar/README.chromium | 4 | ||||
-rw-r--r-- | third_party/handlebar/handlebar.py | 15 |
2 files changed, 16 insertions, 3 deletions
diff --git a/third_party/handlebar/README.chromium b/third_party/handlebar/README.chromium index e057599..a0d7755 100644 --- a/third_party/handlebar/README.chromium +++ b/third_party/handlebar/README.chromium @@ -2,8 +2,8 @@ Name: Handlebar, logic-less templating system Short Name: handlebar URL: https://github.com/kalman/templates Version: 0 -Date: May 30, 2012 -Revision: commit 79b1d45 +Date: June 7, 2012 +Revision: commit 3397712d4bb91a5f5e25637580761f6c565a0cd6 License: Apache License, Version 2.0 Security Critical: no diff --git a/third_party/handlebar/handlebar.py b/third_party/handlebar/handlebar.py index d05e15e..a749ca9 100644 --- a/third_party/handlebar/handlebar.py +++ b/third_party/handlebar/handlebar.py @@ -30,6 +30,17 @@ input = { ] } print(template.render(input).text) + +Handlebar will use get() on contexts to return values, so to create custom +getters (e.g. something that populates values lazily from keys) just add +a get() method. + +class CustomContext(object): + def get(self, key): + return 10 + +# Any time {{ }} is used, will fill it with 10. +print(Handlebar('hello {{world}}').render(CustomContext()).text) """ class ParseException(Exception): @@ -82,7 +93,9 @@ class PathIdentifier(object): def _resolveFrom(self, context): result = context for next in self.path: - if not result or type(result) != dict: + # Only require that contexts provide a get method, meaning that callers + # can provide dict-like contexts (for example, to populate values lazily). + if not result or not getattr(result, "get", None): return None result = result.get(next) return result |