comparison piecrust/templating/jinjaengine.py @ 19:dc72a288921f

Add the `paginate` filter to Jinja, activate `auto_reload`.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 18 Aug 2014 16:53:47 -0700
parents 343d08ef5668
children 19f3ac27c3d5
comparison
equal deleted inserted replaced
18:0170f449f924 19:dc72a288921f
7 from jinja2.ext import Extension, Markup 7 from jinja2.ext import Extension, Markup
8 from jinja2.nodes import CallBlock, Const 8 from jinja2.nodes import CallBlock, Const
9 from pygments import highlight 9 from pygments import highlight
10 from pygments.formatters import HtmlFormatter 10 from pygments.formatters import HtmlFormatter
11 from pygments.lexers import get_lexer_by_name, guess_lexer 11 from pygments.lexers import get_lexer_by_name, guess_lexer
12 from piecrust.data.paginator import Paginator
12 from piecrust.rendering import format_text 13 from piecrust.rendering import format_text
13 from piecrust.routing import CompositeRouteFunction 14 from piecrust.routing import CompositeRouteFunction
14 from piecrust.templating.base import TemplateEngine, TemplateNotFoundError 15 from piecrust.templating.base import TemplateEngine, TemplateNotFoundError
15 16
16 17
67 68
68 class PieCrustEnvironment(Environment): 69 class PieCrustEnvironment(Environment):
69 def __init__(self, app, *args, **kwargs): 70 def __init__(self, app, *args, **kwargs):
70 super(PieCrustEnvironment, self).__init__(*args, **kwargs) 71 super(PieCrustEnvironment, self).__init__(*args, **kwargs)
71 self.app = app 72 self.app = app
73 self.auto_reload = True
72 self.globals.update({ 74 self.globals.update({
73 'fail': raise_exception}) 75 'fail': raise_exception})
74 self.filters.update({ 76 self.filters.update({
77 'paginate': self._paginate,
75 'formatwith': self._formatWith, 78 'formatwith': self._formatWith,
76 'markdown': lambda v: self._formatWith(v, 'markdown'), 79 'markdown': lambda v: self._formatWith(v, 'markdown'),
77 'textile': lambda v: self._formatWith(v, 'textile'), 80 'textile': lambda v: self._formatWith(v, 'textile'),
78 'nocache': add_no_cache_parameter, 81 'nocache': add_no_cache_parameter,
79 'wordcount': get_word_count, 82 'wordcount': get_word_count,
105 else: 108 else:
106 raise Exception("Route function '%s' collides with an " 109 raise Exception("Route function '%s' collides with an "
107 "existing function or template data." % 110 "existing function or template data." %
108 name) 111 name)
109 112
113 def _paginate(self, value, items_per_page=5):
114 cpi = self.app.env.exec_info_stack.current_page_info
115 if cpi is None or cpi.page is None or cpi.render_ctx is None:
116 raise Exception("Can't paginate when no page has been pushed "
117 "on the execution stack.")
118 return Paginator(cpi.page, value, cpi.render_ctx.uri,
119 page_num=cpi.render_ctx.page_num,
120 items_per_page=items_per_page)
121
110 def _formatWith(self, value, format_name): 122 def _formatWith(self, value, format_name):
111 return format_text(self.app, format_name, value) 123 return format_text(self.app, format_name, value)
112 124
113 125
114 def raise_exception(msg): 126 def raise_exception(msg):