[svn] added support for comments and some doc changes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 9061e54 · Browse files · 7 files·+66 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+66 added−7 removed
- other:+66 added−7 removed
Provenance
- Commit
68c8725- Tree
5ade7a9- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
9061e54
7 files changed+66 added−7 removed
added:docs/src/frameworks.txt+33 added
1+=====================2+Framework Integration3+=====================4+5+Jinja registers itself in the baker template plugin system. If your framework6+supports baker (currently `TurboGears`_ and `pylons`_) you can add it very7+easy.8+9+Pylons10+------11+12+Edit ``yourproject/config/middleware.py`` and add the following lines to13+`config.init_app...`:14+15+.. sourcecode:: python16+17+ from jinja import Environment, FileSystemLoader18+19+ config.add_template_engine('jinja', {20+ 'jinja.environment': Environment(loader=FileSystemLoader('path/to/templates'))21+ })22+23+To make Jinja the default template engine change the `init_app` call to24+something like this:25+26+.. sourcecode:: python27+28+ config.init_app(global_conf, app_conf, package='yourproject',29+ template_engine='jinja')30+31+32+.. _TurboGears: http://www.turbogears.org/33+.. _pylons: http://www.pylonshq.com/modified:docs/src/index.txt+2 added
16 unmodified lines17 - `Test Functions <tests.txt>`_18 19 - `Translators <translators.txt>`_20 21+ - `Framework Integration <frameworks.txt>`_22+23 - Template Designer Documentation:24 25 - `Syntax Reference <designerdoc.txt>`_26 1 unmodified linemodified:jinja/translators/python.py+2 added
413 unmodified lines414 defaults.append(self.handle_node(n))415 416 write('def macro(*args):')417 self.indention += 1418+ write('context.push()')419 write('%s = (args + %s[len(args):])' % (_to_tuple(args), _to_tuple(defaults)))420 write('macrobuffer = []')421 write('write = macrobuffer.append')422 buf.append(self.handle_node(node.body))423+ write('context.pop()')424 write('return u\'\'.join(macrobuffer)')425 self.indention -= 1426 buf.append(self.indent('context[%r] = macro' % node.name))427 367 unmodified linesmodified:jinja/datastructure.py+7 added-1 removed
284 unmodified lines285 return False286 return True287 288 eos = property(lambda x: not x.__nonzero__(), doc=__nonzero__.__doc__)289- 289+290 def next(self):291 """Return the next token from the stream."""292 if self._pushed:293 rv = self._pushed.pop()21 unmodified lines315 yield token316 except StopIteration:317 raise IndexError('end of stream reached')318 319+ def drop_until(self, test, drop_needle=False):320+ """Fetch tokens until a function matches and drop all321+ tokens."""322+ for token in self.fetch_until(test, drop_needle):323+ pass324+325 def push(self, lineno, token, data):326 """Push an yielded token back to the stream."""327 self._pushed.append((lineno, token, data))modified:jinja/filters.py+4 added-4 removed
167 unmodified lines168 {{ [1, 2, 3]|join }}169 -> 123170 """171 def wrapped(env, context, value):172- d = env.to_unicode(d)173- return d.join([env.to_unicode(x) for x in value])172+ return env.to_unicode(d).join([env.to_unicode(x) for x in value])173 return wrapped174 175 176 def do_count():4 unmodified lines181 """182 def wrapped(env, context, value):183 try:184 if type(value) in (int, float, long):186- return len(str(var))187- return len(var)185+ return len(str(value))186+ return len(value)187 except TypeError:188 return 0189 return wrapped190 114 unmodified lines305 'title': do_title,306 'default': do_default,307 'join': do_join,308 'count': do_count,309+ 'length': do_count,310 'reverse': do_reverse,311 'center': do_center,312 'title': do_title,313 'capitalize': do_capitalize,6 unmodified linesmodified:jinja/parser.py+10 added-2 removed
17 unmodified lines18 19 # callback functions for the subparse method20 end_of_block = lambda p, t, d: t == 'block_end'21 end_of_variable = lambda p, t, d: t == 'variable_end'22+end_of_comment = lambda p, t, d: t == 'comment_end'23 switch_for = lambda p, t, d: t == 'name' and d in ('else', 'endfor')24 end_of_for = lambda p, t, d: t == 'name' and d == 'endfor'25 switch_if = lambda p, t, d: t == 'name' and d in ('else', 'elif', 'endif')26 end_of_if = lambda p, t, d: t == 'name' and d == 'endif'272 unmodified lines299 plural = indicator = None300 301 while True:302 lineno, token, data = self.tokenstream.next()303+ # comments304+ if token == 'comment_begin':305+ self.tokenstream.drop_until(end_of_comment, True)306 # nested variables303- if token == 'variable_begin':307+ elif token == 'variable_begin':308 _, variable_token, variable_name = self.tokenstream.next()309 if variable_token != 'name' or variable_name not in replacements:310 raise TemplateSyntaxError('unregistered translation '311 'variable %r.' % variable_name,101 unmodified lines413 414 lineno = self.tokenstream.last[0]415 result = nodes.NodeList(lineno)416 for lineno, token, data in self.tokenstream:417+ # comments418+ if token == 'comment_begin':419+ self.tokenstream.drop_until(end_of_comment, True)420+421 # this token marks the begin or a variable section.422 # parse everything till the end of it.415- if token == 'variable_begin':423+ elif token == 'variable_begin':424 gen = self.tokenstream.fetch_until(end_of_variable, True)425 result.append(self.directives['print'](lineno, gen))426 427 # this token marks the start of a block. like for variables61 unmodified linesadded:TODO+8 added
1+===================2+TODO List for Jinja3+===================4+5+- Requirements in Jinja (blocks and set directives) outside of renderable6+ blocks should become part of the module not the generate function.7+8+- Improve the context lookup (maybe with an optional C extension)