Commit d68ae32f by Chris Wanstrath

Add failing (but almost passing) ComplexView test. Fixes #2

parent b186f009
<h1>{{header}}</h1>
{{#list}}
<ul>
{{#item}}
{{#current}}
<li><strong>{{name}}</strong></li>
{{/current}}
{{#link}}
<li><a href="{{url}}">{{name}}</a></li>
{{/link}}
{{/item}}
</ul>
{{/list}}
{{#empty}}
<p>The list is empty.</p>
{{/empty}}
\ No newline at end of file
import pystache
class ComplexView(pystache.View):
template_path = 'examples'
def header(self):
return "Colors"
def item(self):
items = []
items.append({ 'name': 'red', 'current': True, 'url': '#Red' })
items.append({ 'name': 'green', 'link': True, 'url': '#Green' })
items.append({ 'name': 'blue', 'link': True, 'url': '#Blue' })
return items
def list(self):
return not self.empty()
def empty(self):
return len(self.item()) == 0
......@@ -30,16 +30,14 @@ class Template(object):
section, section_name, inner = match.group(0, 1, 2)
it = context.get(section_name)
it = context.get(section_name, None)
replacer = ''
if it and not hasattr(it, '__iter__'):
replacer = inner
elif it:
insides = []
for item in context[section_name]:
ctx = context.copy()
ctx.update(item)
insides.append(self.render(inner, ctx))
for item in it:
insides.append(self.render(inner, item))
replacer = ''.join(insides)
template = template.replace(section, replacer)
......
from pystache import Template
import os.path
import re
class View(object):
# Path where this view's template(s) live
......@@ -33,8 +34,18 @@ class View(object):
f.close()
return template
def template_name(self):
return self.__class__.__name__
def template_name(self, name=None):
"""TemplatePartial => template_partial
Takes a string but defaults to using the current class' name.
"""
if not name:
name = self.__class__.__name__
def repl(match):
return '_' + match.group(0).lower()
return re.sub('[A-Z]', repl, name)[1:]
def get(self, attr, default):
if attr in self.context:
......
import unittest
import pystache
from examples.simple import Simple
from examples.complex_view import ComplexView
class TestView(unittest.TestCase):
def test_basic(self):
......@@ -19,3 +20,11 @@ class TestView(unittest.TestCase):
view = Simple()
self.assertEquals(view.render(), "Hi pizza!")
def test_complex(self):
self.assertEquals(ComplexView().render(), """<h1>Colors</h1>
<ul>
<li><strong>red</strong></li>
<li><a href="#Green">green</a></li>
<li><a href="#Blue">blue</a></li>
</ul>
""")
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