Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improved choice and prefix loaders.

Choice and prefix loaders now dispatch source and template lookup
separately in order to work in combination with module loaders as
advertised.
  • Loading branch information...
commit 6fa6cb0e129893b0166448563597e1d156cd8746 1 parent 9b1de00
Armin Ronacher mitsuhiko authored
8 CHANGES
... ... @@ -1,6 +1,14 @@
1 1 Jinja2 Changelog
2 2 ================
3 3
  4 +Version 2.7
  5 +-----------
  6 +(codename to be selected, release date to be announced)
  7 +
  8 +- Choice and prefix loaders now dispatch source and template lookup
  9 + separately in order to work in combination with module loaders as
  10 + advertised.
  11 +
4 12 Version 2.6
5 13 -----------
6 14 (codename Convolution, released on July 24th 2011)
25 jinja2/loaders.py
@@ -330,12 +330,16 @@ def __init__(self, mapping, delimiter='/'):
330 330 self.mapping = mapping
331 331 self.delimiter = delimiter
332 332
333   - def get_source(self, environment, template):
  333 + def get_loader(self, template):
334 334 try:
335 335 prefix, name = template.split(self.delimiter, 1)
336 336 loader = self.mapping[prefix]
337 337 except (ValueError, KeyError):
338 338 raise TemplateNotFound(template)
  339 + return loader, name
  340 +
  341 + def get_source(self, environment, template):
  342 + loader, name = self.get_loader(template)
339 343 try:
340 344 return loader.get_source(environment, name)
341 345 except TemplateNotFound:
@@ -343,6 +347,16 @@ def get_source(self, environment, template):
343 347 # (the one that includes the prefix)
344 348 raise TemplateNotFound(template)
345 349
  350 + @internalcode
  351 + 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)
  359 +
346 360 def list_templates(self):
347 361 result = []
348 362 for prefix, loader in self.mapping.iteritems():
@@ -376,6 +390,15 @@ def get_source(self, environment, template):
376 390 pass
377 391 raise TemplateNotFound(template)
378 392
  393 + @internalcode
  394 + 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 + pass
  400 + raise TemplateNotFound(name)
  401 +
379 402 def list_templates(self):
380 403 found = set()
381 404 for loader in self.loaders:
28 jinja2/testsuite/loader.py
@@ -182,6 +182,34 @@ def test_byte_compilation(self):
182 182 tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490
183 183 assert mod.__file__.endswith('.pyc')
184 184
  185 + def test_choice_loader(self):
  186 + log = self.compile_down(py_compile=True)
  187 + assert 'Byte-compiled "a/test.html"' in log
  188 +
  189 + self.mod_env.loader = loaders.ChoiceLoader([
  190 + self.mod_env.loader,
  191 + loaders.DictLoader({'DICT_SOURCE': 'DICT_TEMPLATE'})
  192 + ])
  193 +
  194 + 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')
  198 +
  199 + def test_prefix_loader(self):
  200 + log = self.compile_down(py_compile=True)
  201 + assert 'Byte-compiled "a/test.html"' in log
  202 +
  203 + self.mod_env.loader = loaders.PrefixLoader({
  204 + 'MOD': self.mod_env.loader,
  205 + 'DICT': loaders.DictLoader({'test.html': 'DICT_TEMPLATE'})
  206 + })
  207 +
  208 + 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')
  212 +
185 213
186 214 def suite():
187 215 suite = unittest.TestSuite()

0 comments on commit 6fa6cb0

Please sign in to comment.
Something went wrong with that request. Please try again.