summaryrefslogtreecommitdiff
path: root/pystache/__init__.py
diff options
context:
space:
mode:
authorChris Wanstrath <chris@ozmm.org>2009-10-28 23:58:44 -0700
committerChris Wanstrath <chris@ozmm.org>2009-10-28 23:58:44 -0700
commit406253878ae20203f5944ac853dbcad316e59cd6 (patch)
tree79ba3537041ccab1905d3ddf141a88557ee5b079 /pystache/__init__.py
parent0ed75fa0faf449086bd28a2ca2a5d9ebbfb6f44d (diff)
downloadpystache-406253878ae20203f5944ac853dbcad316e59cd6.tar.gz
simple, but tests pass
Diffstat (limited to 'pystache/__init__.py')
-rw-r--r--pystache/__init__.py28
1 files changed, 28 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
+