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): ...@@ -30,16 +30,14 @@ class Template(object):
section, section_name, inner = match.group(0, 1, 2) section, section_name, inner = match.group(0, 1, 2)
it = context.get(section_name) it = context.get(section_name, None)
replacer = '' replacer = ''
if it and not hasattr(it, '__iter__'): if it and not hasattr(it, '__iter__'):
replacer = inner replacer = inner
elif it: elif it:
insides = [] insides = []
for item in context[section_name]: for item in it:
ctx = context.copy() insides.append(self.render(inner, item))
ctx.update(item)
insides.append(self.render(inner, ctx))
replacer = ''.join(insides) replacer = ''.join(insides)
template = template.replace(section, replacer) template = template.replace(section, replacer)
......
from pystache import Template from pystache import Template
import os.path import os.path
import re
class View(object): class View(object):
# Path where this view's template(s) live # Path where this view's template(s) live
...@@ -33,8 +34,18 @@ class View(object): ...@@ -33,8 +34,18 @@ class View(object):
f.close() f.close()
return template return template
def template_name(self): def template_name(self, name=None):
return self.__class__.__name__ """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): def get(self, attr, default):
if attr in self.context: if attr in self.context:
......
import unittest import unittest
import pystache import pystache
from examples.simple import Simple from examples.simple import Simple
from examples.complex_view import ComplexView
class TestView(unittest.TestCase): class TestView(unittest.TestCase):
def test_basic(self): def test_basic(self):
...@@ -19,3 +20,11 @@ class TestView(unittest.TestCase): ...@@ -19,3 +20,11 @@ class TestView(unittest.TestCase):
view = Simple() view = Simple()
self.assertEquals(view.render(), "Hi pizza!") 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