diff options
author | Chris Wanstrath <chris@ozmm.org> | 2009-10-29 01:01:50 -0700 |
---|---|---|
committer | Chris Wanstrath <chris@ozmm.org> | 2009-10-29 01:01:50 -0700 |
commit | a28dfaefcb3b64906b9bcfeb21dee67abd4dd108 (patch) | |
tree | 8bc0057e58a54f2441527de81a2c09d2a207e810 | |
parent | 1bd85306567d828140c2def90bc14bae475d9f92 (diff) | |
download | pystache-a28dfaefcb3b64906b9bcfeb21dee67abd4dd108.tar.gz |
move Template into its own file
-rw-r--r-- | pystache/__init__.py | 63 | ||||
-rw-r--r-- | pystache/template.py | 61 |
2 files changed, 62 insertions, 62 deletions
diff --git a/pystache/__init__.py b/pystache/__init__.py index a01b9f1..c838c7c 100644 --- a/pystache/__init__.py +++ b/pystache/__init__.py @@ -1,67 +1,6 @@ -import re +from pystache.template import Template class Pystache(object): @staticmethod def render(template, context={}): return Template(template, context).render() - -class Template(object): - tag_types = { - None: 'tag', - '!': 'comment' - } - - def __init__(self, template, context={}): - self.template = template - self.context = context - - def render(self, template=None, context=None): - """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?}}+") - - match = re.search(regexp, template) - while match: - tag, tag_type, tag_name = match.group(0, 1, 2) - func = 'render_' + self.tag_types[tag_type] - - if hasattr(self, func): - replacement = getattr(self, func)(tag_name, context) - template = template.replace(tag, replacement) - - match = re.search(regexp, template) - - return template - - def render_tag(self, tag_name, context): - """Given a tag name and context, finds and renders the tag.""" - if tag_name in context: - return context[tag_name] - else: - return '' - - def render_comment(self, tag_name=None, context=None): - """Rendering a comment always returns nothing.""" - return '' - diff --git a/pystache/template.py b/pystache/template.py new file mode 100644 index 0000000..7214fb8 --- /dev/null +++ b/pystache/template.py @@ -0,0 +1,61 @@ +import re + +class Template(object): + tag_types = { + None: 'tag', + '!': 'comment' + } + + def __init__(self, template, context={}): + self.template = template + self.context = context + + def render(self, template=None, context=None): + """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?}}+") + + match = re.search(regexp, template) + while match: + tag, tag_type, tag_name = match.group(0, 1, 2) + func = 'render_' + self.tag_types[tag_type] + + if hasattr(self, func): + replacement = getattr(self, func)(tag_name, context) + template = template.replace(tag, replacement) + + match = re.search(regexp, template) + + return template + + def render_tag(self, tag_name, context): + """Given a tag name and context, finds and renders the tag.""" + if tag_name in context: + return context[tag_name] + else: + return '' + + def render_comment(self, tag_name=None, context=None): + """Rendering a comment always returns nothing.""" + return '' |