Skip to content
Myr

Improved choice and prefix loaders.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Choice and prefix loaders now dispatch source and template lookup separately in order to work in combination with module loaders as advertised.

authored by Armin Ronacher · committed by Armin Ronacher · parent 9b1de00 · Browse files · 3 files+60 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+60 added−1 removed

  • other:+60 added−1 removed

3 files+60 added−1 removed

modified:jinja2/testsuite/loader.py+28 added
180 unmodified lines
181 mod = self.mod_env.loader.module. \182 tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490183 assert mod.__file__.endswith('.pyc')184185 def test_choice_loader(self):186 log = self.compile_down(py_compile=True)187 assert 'Byte-compiled "a/test.html"' in log188189 self.mod_env.loader = loaders.ChoiceLoader([190 self.mod_env.loader,191 loaders.DictLoader({'DICT_SOURCE': 'DICT_TEMPLATE'})192 ])193194 tmpl1 = self.mod_env.get_template('a/test.html')195 self.assert_equal(tmpl1.render(), 'BAR')196 tmpl2 = self.mod_env.get_template('DICT_SOURCE')197 self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')198199 def test_prefix_loader(self):200 log = self.compile_down(py_compile=True)201 assert 'Byte-compiled "a/test.html"' in log202203 self.mod_env.loader = loaders.PrefixLoader({204 'MOD': self.mod_env.loader,205 'DICT': loaders.DictLoader({'test.html': 'DICT_TEMPLATE'})206 })207208 tmpl1 = self.mod_env.get_template('MOD/a/test.html')209 self.assert_equal(tmpl1.render(), 'BAR')210 tmpl2 = self.mod_env.get_template('DICT/test.html')211 self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')212213214def suite():215 suite = unittest.TestSuite()216 suite.addTest(unittest.makeSuite(LoaderTestCase))217 suite.addTest(unittest.makeSuite(ModuleLoaderTestCase))
1 unmodified line
modified:jinja2/loaders.py+24 added-1 removed
328 unmodified lines
329 def __init__(self, mapping, delimiter='/'):330 self.mapping = mapping331 self.delimiter = delimiter332333 def get_source(self, environment, template):333 def get_loader(self, template):334 try:335 prefix, name = template.split(self.delimiter, 1)336 loader = self.mapping[prefix]337 except (ValueError, KeyError):338 raise TemplateNotFound(template)339 return loader, name340341 def get_source(self, environment, template):342 loader, name = self.get_loader(template)343 try:344 return loader.get_source(environment, name)345 except TemplateNotFound:346 # re-raise the exception with the correct fileame here.347 # (the one that includes the prefix)348 raise TemplateNotFound(template)349350 @internalcode351 def load(self, environment, name, globals=None):352 loader, local_name = self.get_loader(name)353 try:354 return loader.load(environment, local_name)355 except TemplateNotFound:356 # re-raise the exception with the correct fileame here.357 # (the one that includes the prefix)358 raise TemplateNotFound(name)359360 def list_templates(self):361 result = []362 for prefix, loader in self.mapping.iteritems():363 for template in loader.list_templates():
25 unmodified lines
389 except TemplateNotFound:390 pass391 raise TemplateNotFound(template)392393 @internalcode394 def load(self, environment, name, globals=None):395 for loader in self.loaders:396 try:397 return loader.load(environment, name, globals)398 except TemplateNotFound:399 pass400 raise TemplateNotFound(name)401402 def list_templates(self):403 found = set()404 for loader in self.loaders:405 found.update(loader.list_templates())
68 unmodified lines
modified:CHANGES+8 added
1Jinja2 Changelog2================34Version 2.75-----------6(codename to be selected, release date to be announced)78- Choice and prefix loaders now dispatch source and template lookup9 separately in order to work in combination with module loaders as10 advertised.1112Version 2.613-----------14(codename Convolution, released on July 24th 2011)15
262 unmodified lines