Commit 40b904a3 by Chris Wanstrath

implement partials, make test pass

parent d02b657f
...@@ -8,7 +8,8 @@ class Template(object): ...@@ -8,7 +8,8 @@ class Template(object):
tag_types = { tag_types = {
None: 'tag', None: 'tag',
'!': 'comment', '!': 'comment',
'{': 'unescaped' '{': 'unescaped',
'>': 'partial'
} }
def __init__(self, template, context=None): def __init__(self, template, context=None):
...@@ -63,7 +64,7 @@ class Template(object): ...@@ -63,7 +64,7 @@ class Template(object):
return template return template
def render_tag(self, tag_name, context): def render_tag(self, tag_name, context):
"""Given a tag name and context, finds and renders the tag.""" """Given a tag name and context, finds, escapes, and renders the tag."""
return cgi.escape(context.get(tag_name, '')) return cgi.escape(context.get(tag_name, ''))
def render_comment(self, tag_name=None, context=None): def render_comment(self, tag_name=None, context=None):
...@@ -73,3 +74,13 @@ class Template(object): ...@@ -73,3 +74,13 @@ class Template(object):
def render_unescaped(self, tag_name=None, context=None): def render_unescaped(self, tag_name=None, context=None):
"""Render a tag without escaping it.""" """Render a tag without escaping it."""
return context.get(tag_name, '') return context.get(tag_name, '')
def render_partial(self, tag_name=None, context=None):
"""Renders a partial within the current context."""
# Import view here to avoid import loop
from pystache.view import View
view = View(context=context)
view.template_name = tag_name
return view.render()
...@@ -9,6 +9,10 @@ class View(object): ...@@ -9,6 +9,10 @@ class View(object):
# Extension for templates # Extension for templates
template_extension = 'mustache' template_extension = 'mustache'
# The name of this template. If none is given the View will try
# to infer it based on the class name.
template_name = None
# Absolute path to the template itself. Pystache will try to guess # Absolute path to the template itself. Pystache will try to guess
# if it's not provided. # if it's not provided.
template_file = None template_file = None
...@@ -19,14 +23,35 @@ class View(object): ...@@ -19,14 +23,35 @@ class View(object):
def __init__(self, template=None, context=None, **kwargs): def __init__(self, template=None, context=None, **kwargs):
self.template = template self.template = template
self.context = context or {} self.context = context or {}
self.context.update(kwargs)
# If the context we're handed is a View, we want to inherit
# its settings.
if isinstance(context, View):
self.inherit_settings(context)
if kwargs:
self.context.update(kwargs)
def inherit_settings(self, view):
"""Given another View, copies its settings."""
if view.template_path:
self.template_path = view.template_path
if view.template_name:
self.template_name = view.template_name
def __contains__(self, needle):
return hasattr(self, needle)
def __getitem__(self, attr):
return getattr(self, attr)()
def load_template(self): def load_template(self):
if self.template: if self.template:
return self.template return self.template
if not self.template_file: if not self.template_file:
name = self.template_name() + '.' + self.template_extension name = self.get_template_name() + '.' + self.template_extension
self.template_file = os.path.join(self.template_path, name) self.template_file = os.path.join(self.template_path, name)
f = open(self.template_file, 'r') f = open(self.template_file, 'r')
...@@ -34,10 +59,14 @@ class View(object): ...@@ -34,10 +59,14 @@ class View(object):
f.close() f.close()
return template return template
def template_name(self, name=None): def get_template_name(self, name=None):
"""TemplatePartial => template_partial """TemplatePartial => template_partial
Takes a string but defaults to using the current class' name. Takes a string but defaults to using the current class' name or
the `template_name` attribute
""" """
if self.template_name:
return self.template_name
if not name: if not name:
name = self.__class__.__name__ name = self.__class__.__name__
......
...@@ -24,4 +24,5 @@ class TestView(unittest.TestCase): ...@@ -24,4 +24,5 @@ class TestView(unittest.TestCase):
self.assertEquals(Unescaped().render(), "<h1>Bear > Shark</h1>") self.assertEquals(Unescaped().render(), "<h1>Bear > Shark</h1>")
def test_template_partial(self): def test_template_partial(self):
self.assertEquals(TemplatePartial().render(), 'blah') self.assertEquals(TemplatePartial().render(), """<h1>Welcome</h1>
Again, Welcome!""")
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