From 406253878ae20203f5944ac853dbcad316e59cd6 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Wed, 28 Oct 2009 23:58:44 -0700 Subject: simple, but tests pass --- pystache/__init__.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 pystache/__init__.py (limited to 'pystache/__init__.py') 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 + -- cgit v1.2.1