Commit 1b31b0dc by Pieter van de Bruggen

Adding basic support for partials.

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