Mercurial > piecrust2
comparison piecrust/templating/jinjaengine.py @ 802:0da1207472d3
templating: Put the routing functions in the data, not the template engine.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 29 Sep 2016 21:52:18 -0700 |
parents | f6f9a284a5f3 |
children | 8f8bbb2e70e1 |
comparison
equal
deleted
inserted
replaced
801:87df68be4cc8 | 802:0da1207472d3 |
---|---|
15 from pygments.formatters import HtmlFormatter | 15 from pygments.formatters import HtmlFormatter |
16 from pygments.lexers import get_lexer_by_name, guess_lexer | 16 from pygments.lexers import get_lexer_by_name, guess_lexer |
17 from piecrust.data.paginator import Paginator | 17 from piecrust.data.paginator import Paginator |
18 from piecrust.environment import AbortedSourceUseError | 18 from piecrust.environment import AbortedSourceUseError |
19 from piecrust.rendering import format_text | 19 from piecrust.rendering import format_text |
20 from piecrust.routing import CompositeRouteFunction | |
21 from piecrust.templating.base import (TemplateEngine, TemplateNotFoundError, | 20 from piecrust.templating.base import (TemplateEngine, TemplateNotFoundError, |
22 TemplatingError) | 21 TemplatingError) |
23 from piecrust.uriutil import multi_replace | 22 from piecrust.uriutil import multi_replace |
24 | 23 |
25 | 24 |
231 | 230 |
232 # Backwards compatibility with Twig. | 231 # Backwards compatibility with Twig. |
233 if twig_compatibility_mode is True: | 232 if twig_compatibility_mode is True: |
234 self.filters['raw'] = self.filters['safe'] | 233 self.filters['raw'] = self.filters['safe'] |
235 self.globals['pcfail'] = raise_exception | 234 self.globals['pcfail'] = raise_exception |
236 | |
237 # Add route functions. | |
238 for route in app.routes: | |
239 name = route.func_name | |
240 func = self.globals.get(name) | |
241 if func is None: | |
242 func = CompositeRouteFunction() | |
243 func.addFunc(route) | |
244 self.globals[name] = func | |
245 elif isinstance(func, CompositeRouteFunction): | |
246 self.globals[name].addFunc(route) | |
247 else: | |
248 raise Exception("Route function '%s' collides with an " | |
249 "existing function or template data." % | |
250 name) | |
251 | 235 |
252 def _paginate(self, value, items_per_page=5): | 236 def _paginate(self, value, items_per_page=5): |
253 cpi = self.app.env.exec_info_stack.current_page_info | 237 cpi = self.app.env.exec_info_stack.current_page_info |
254 if cpi is None or cpi.page is None or cpi.render_ctx is None: | 238 if cpi is None or cpi.page is None or cpi.render_ctx is None: |
255 raise Exception("Can't paginate when no page has been pushed " | 239 raise Exception("Can't paginate when no page has been pushed " |