mirror of
https://github.com/kennethreitz-archive/pystache.git
synced 2026-06-05 23:40:16 +00:00
Adding basic support for partials.
This commit is contained in:
@@ -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
|
||||
|
||||
+13
-2
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user