Adding basic support for partials.

This commit is contained in:
Pieter van de Bruggen
2011-02-21 12:41:07 -08:00
parent bd24077a50
commit 1b31b0dc4a
3 changed files with 20 additions and 3 deletions
+3 -1
View File
@@ -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
+4
View File
@@ -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
+13 -2
View File
@@ -1,6 +1,7 @@
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)