# HG changeset patch # User Ludovic Chabant # Date 1408406027 25200 # Node ID dc72a288921fbd04b8662450668c8aca48289f18 # Parent 0170f449f924c651a188108f491ec557ab7ca5b7 Add the `paginate` filter to Jinja, activate `auto_reload`. diff -r 0170f449f924 -r dc72a288921f piecrust/templating/jinjaengine.py --- a/piecrust/templating/jinjaengine.py Mon Aug 18 16:53:26 2014 -0700 +++ b/piecrust/templating/jinjaengine.py Mon Aug 18 16:53:47 2014 -0700 @@ -9,6 +9,7 @@ from pygments import highlight from pygments.formatters import HtmlFormatter from pygments.lexers import get_lexer_by_name, guess_lexer +from piecrust.data.paginator import Paginator from piecrust.rendering import format_text from piecrust.routing import CompositeRouteFunction from piecrust.templating.base import TemplateEngine, TemplateNotFoundError @@ -69,9 +70,11 @@ def __init__(self, app, *args, **kwargs): super(PieCrustEnvironment, self).__init__(*args, **kwargs) self.app = app + self.auto_reload = True self.globals.update({ 'fail': raise_exception}) self.filters.update({ + 'paginate': self._paginate, 'formatwith': self._formatWith, 'markdown': lambda v: self._formatWith(v, 'markdown'), 'textile': lambda v: self._formatWith(v, 'textile'), @@ -107,6 +110,15 @@ "existing function or template data." % name) + def _paginate(self, value, items_per_page=5): + cpi = self.app.env.exec_info_stack.current_page_info + if cpi is None or cpi.page is None or cpi.render_ctx is None: + raise Exception("Can't paginate when no page has been pushed " + "on the execution stack.") + return Paginator(cpi.page, value, cpi.render_ctx.uri, + page_num=cpi.render_ctx.page_num, + items_per_page=items_per_page) + def _formatWith(self, value, format_name): return format_text(self.app, format_name, value)