Commit 40625387 by Chris Wanstrath

simple, but tests pass

parent 0ed75fa0
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
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?")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment