diff options
author | Chris Wanstrath <chris@ozmm.org> | 2009-10-28 23:58:44 -0700 |
---|---|---|
committer | Chris Wanstrath <chris@ozmm.org> | 2009-10-28 23:58:44 -0700 |
commit | 406253878ae20203f5944ac853dbcad316e59cd6 (patch) | |
tree | 79ba3537041ccab1905d3ddf141a88557ee5b079 | |
parent | 0ed75fa0faf449086bd28a2ca2a5d9ebbfb6f44d (diff) | |
download | pystache-406253878ae20203f5944ac853dbcad316e59cd6.tar.gz |
simple, but tests pass
-rw-r--r-- | pystache/__init__.py | 28 | ||||
-rw-r--r-- | pystache/__init__.pyc | bin | 0 -> 1588 bytes | |||
-rw-r--r-- | tests/test_pystache.py | 14 | ||||
-rw-r--r-- | tests/test_pystache.pyc | bin | 0 -> 1129 bytes |
4 files changed, 42 insertions, 0 deletions
diff --git a/pystache/__init__.py b/pystache/__init__.py new file mode 100644 index 0000000..2f023a8 --- /dev/null +++ b/pystache/__init__.py @@ -0,0 +1,28 @@ +import re + +class Pystache(object): + @staticmethod + def render(template, context): + return Template(template, context).render() + +class Template(object): + def __init__(self, template, context={}): + self.template = template + self.context = context + + def render(self): + return self.render_tags() + + def render_tags(self): + regexp = re.compile(r"{{(#|=|!|<|>|\{)?(.+?)\1?}}+") + template = self.template + + match = re.search(regexp, template) + while match: + tag, tag_name = match.group(0, 2) + if tag_name in self.context: + template = template.replace(tag, self.context[tag_name]) + match = re.search(regexp, template) + + return template + diff --git a/pystache/__init__.pyc b/pystache/__init__.pyc Binary files differnew file mode 100644 index 0000000..992e021 --- /dev/null +++ b/pystache/__init__.pyc diff --git a/tests/test_pystache.py b/tests/test_pystache.py new file mode 100644 index 0000000..c3fb775 --- /dev/null +++ b/tests/test_pystache.py @@ -0,0 +1,14 @@ +import unittest +from pystache import Pystache + +class TestPystache(unittest.TestCase): + def test_basic(self): + ret = Pystache.render("Hi {{thing}}!", { 'thing': 'world' }) + self.assertEquals(ret, "Hi world!") + + def test_less_basic(self): + template = """It's a nice day for {{beverage}}, right {{person}}?""" + ret = Pystache.render(template, { 'beverage': 'soda', 'person': 'Bob' }) + self.assertEquals(ret, "It's a nice day for soda, right Bob?") + + diff --git a/tests/test_pystache.pyc b/tests/test_pystache.pyc Binary files differnew file mode 100644 index 0000000..57c7632 --- /dev/null +++ b/tests/test_pystache.pyc |