diff options
author | Chris Wanstrath <chris@ozmm.org> | 2009-10-29 01:00:15 -0700 |
---|---|---|
committer | Chris Wanstrath <chris@ozmm.org> | 2009-10-29 01:00:15 -0700 |
commit | 1bd85306567d828140c2def90bc14bae475d9f92 (patch) | |
tree | 2a8a72b1b0772b3f1c2869115c7316b6b202b3da /pystache/__init__.py | |
parent | b51bd2e5e38703a42e3b858f93aea5b0146a2604 (diff) | |
download | pystache-1bd85306567d828140c2def90bc14bae475d9f92.tar.gz |
basic true / false sections
Diffstat (limited to 'pystache/__init__.py')
-rw-r--r-- | pystache/__init__.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/pystache/__init__.py b/pystache/__init__.py index 15f6620..a01b9f1 100644 --- a/pystache/__init__.py +++ b/pystache/__init__.py @@ -19,8 +19,24 @@ class Template(object): """Turns a Mustache template into something wonderful.""" template = template or self.template context = context or self.context + + template = self.render_sections(template, context) return self.render_tags(template, context) + def render_sections(self, template, context): + regexp = re.compile(r"{{\#([^\}]*)}}\s*(.+?){{/\1}}") + match = re.search(regexp, template) + + while match: + section, section_name, inner = match.group(0, 1, 2) + if section_name in context and context[section_name]: + return template.replace(section, inner) + else: + return template.replace(section, '') + match = re.search(regexp, template) + + return template + def render_tags(self, template, context): """Renders all the tags in a template for a context.""" regexp = re.compile(r"{{(#|=|!|<|>|\{)?(.+?)\1?}}+") |