Commit 1b31b0dc by Pieter van de Bruggen

Adding basic support for partials.

parent bd24077a
......@@ -82,12 +82,14 @@ class Template(object):
elif captures['tag'] == '=':
self.otag, self.ctag = captures['name'].split()
self._compile_regexps()
elif captures['tag'] == '>':
buffer += self._parse(self.view.partial(captures['name']))
elif captures['tag'] in ['{', '&']:
buffer.append(lambda view: unicode(fetch(view)))
elif captures['tag'] == '':
buffer.append(lambda view: cgi.escape(unicode(fetch(view)), True))
else:
raise
raise Exception("'%s' is an unrecognized type!" % (captures['tag']))
return pos
......
......@@ -49,6 +49,10 @@ class View(object):
return self.template
def partial(self, name):
from pystache import Loader
return Loader().load_template(name, self.template_path, encoding=self.template_encoding, extension=self.template_extension)
def _get_template_name(self, template_name=None):
"""TemplatePartial => template_partial
Takes a string but defaults to using the current class' name or
......
import glob
import os.path
import pystache
from pystache import Loader
import unittest
import yaml
......@@ -19,10 +20,20 @@ class MustacheSpec(unittest.TestCase):
def buildTest(testData, spec):
def test(self):
template = testData['template']
partials = testData.has_key('partials') and test['partials'] or {}
partials = testData.has_key('partials') and testData['partials'] or {}
expected = testData['expected']
data = testData['data']
self.assertEquals(pystache.render(template, data), expected)
files = []
try:
for key in partials.keys():
filename = "%s.%s" % (key, Loader.template_extension)
files.append(os.path.join(Loader.template_path, filename))
p = open(files[-1], 'w')
p.write(partials[key])
self.assertEquals(pystache.render(template, data), expected)
finally:
[os.remove(f) for f in files]
test.__doc__ = testData['desc']
test.__name__ = 'test %s (%s)' % (testData['name'], spec)
......
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