Skip to content
Myr

fixed a bug in error reporting and some small documentation updates

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 0c4f757 · Browse files · 7 files+63 added−17 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+63 added−17 removed

  • other:+63 added−17 removed

7 files+63 added−17 removed

modified:docs/api.rst+42 added-2 removed
13 unmodified lines
14Jinja2 uses a central object called the template :class:`Environment`.15Instances of this class are used to store the configuration, global objects16and are used to load templates from the file system or other locations.17Even if you are creating templates from string by using the constructor of18:class:`Template` class, an environment is created automatically for you.18:class:`Template` class, an environment is created automatically for you,19albeit a shared one.2021Most applications will create one :class:`Environment` object on application22initialization and use that to load templates. In some cases it's however23useful to have multiple environments side by side, if different configurations
19 unmodified lines
43To render it with some variables, just call the :meth:`render` method::4445 print template.render(the='variables', go='here')4647Using a template loader rather then passing strings to :class:`Template`48or :meth:`Environment.from_string` has multiple advantages. Besides being49a lot easier to use it also enables template inheritance.505152Unicode53-------5455Jinja2 is using unicode internally which means that you have to pass unicode56objects to the render function or bytestrings that only consist of ASCII57characters. Additionally newlines are normalized to one end of line58sequence which is per default UNIX style (``\n``).5960Python 2.x supports two ways of representing string objects. One is the61`str` type and the other is the `unicode` type, both of which extend a type62called `basestring`. Unfortunately the default is `str` which should not63be used to store text based information unless only ASCII characters are64used. With Python 2.6 it is possible to my `unicode` the default on a per65module level and with Python 3 it will be the default.6667To explicitly use a unicode string you have to prefix the string literal68with a `u`: ``u'Hänsel und Gretel sagen Hallo'``. That way Python will69store the string as unicode by decoding the string with the character70encoding from the current Python module. If no encoding is specified this71defaults to 'ASCII' which means that you can't use any non ASCII identifier.7273To set a better module encoding add the following comment to the first or74second line of the Python module using the unicode literal::7576 # -*- coding: utf-8 -*-7778We recommend utf-8 as Encoding for Python modules and templates as it's79possible to represent every Unicode character in utf-8 and because it's80backwards compatible to ASCII. For Jinja2 the default encoding of templates81is assumed to be utf-8.8283It is not possible to use Jinja2 to process non unicode data. The reason84for this is that Jinja2 uses Unicode already on the language level. For85example Jinja2 treats the non-breaking space as valid whitespace inside86expressions which requires knowledge of the encoding or operating on an87unicode string.8889For more details about unicode in Python have a look at the excellent90`Unicode documentation`_.919293.. _Unicode documentation: http://docs.python.org/dev/howto/unicode.html9495High Level API96--------------9798.. autoclass:: Environment([options])
387 unmodified lines
486-------------487488The low level API exposes functionality that can be useful to understand some489implementation details, debugging purposes or advanced :ref:`extension451<jinja-extensions>` techniques.490<jinja-extensions>` techniques. Unless you know exactly what you are doing we491don't recommend using any of those.492493.. automethod:: Environment.lex494495.. automethod:: Environment.parse
25 unmodified lines
modified:docs/faq.rst+4 added-4 removed
19 unmodified lines
20benchmark different engines in different situations. The benchmarks from the21testsuite show that Jinja2 has a similar performance to `Mako`_ and is between2210 and 20 times faster than Django's template engine or Genshi. These numbers23should be taken with tons of salt as the benchmarks that took these numbers24only test a few performance related situations such as looping. They are25not a good indicator for the templates used in the average application.26Additionally you should keep in mind that for most web applications27templates are clearly not the bottleneck.24only test a few performance related situations such as looping. Generally25speaking the performance of a template engine doesn't matter much as the26usual bottleneck in a web application is either the database or the application27code.2829.. _Mako: http://www.makotemplates.org/3031How Compatible is Jinja2 with Django?
116 unmodified lines
modified:docs/intro.rst+9 added-3 removed
32 unmodified lines
33-------------3435Jinja2 needs at least **Python 2.4** to run. Additionally a working C-compiler36that can create python extensions should be installed for the debugger. If no37C-compiler is available the `ctypes`_ module should be installed.37C-compiler is available and you are using Python 2.4 the `ctypes`_ module38should be installed.3940.. _ctypes: http://python.net/crew/theller/ctypes/4142
54 unmodified lines
9798This section gives you a brief introduction to the Python API for Jinja2 templates.99100The most basic way to create a template and render it is through100:class:`Template`. This however is not the recommended way to work with it,101but the easiest101:class:`Template`. This however is not the recommended way to work with it if102your templates are not loaded from strings but the file system or another data103source:104105>>> from jinja2 import Template106>>> template = Template('Hello {{ name }}!')107>>> template.render(name='John Doe')
3 unmodified lines
111object that provides a method called :meth:`~Template.render` which when112called with a dict or keyword arguments expands the template. The dict113or keywords arguments passed to the template are the so-called "context"114of the template.115116What you can see here is that Jinja2 is using unicode internally and the117return value is an unicode string. So make sure that your application is118indeed using unicode internally.
modified:docs/templates.rst+5 added-4 removed
147 unmodified lines
148a list of numbers from ``1`` to ``9`` the output would be ``123456789``.149150Note that you must not use a whitespace between the tag and the minus sign:151152 valid:152 valid::153154 {%- if foo -%}...{% endif %}155155 invalid:156 invalid::157158 {% - if foo - %}...{% endif %}159160If :ref:`line-statements` are enabled they strip leading whitespace
871 unmodified lines
1032For multiple placeholders always use keyword arguments to `format` as other1033languages may not use the words in the same order.103410351035do1036~~1036Expression Statement1037~~~~~~~~~~~~~~~~~~~~10381039If the expression-statement extension is loaded a tag called `do` is available1040that works exactly like the regular variable expression (``{{ ... }}``) just1041that it doesn't print anything. This can be used to modify lists::
24 unmodified lines
modified:jinja2/loaders.py+1 added-1 removed
253 unmodified lines
254255 >>> loader = ChoiceLoader([256 ... FileSystemLoader('/path/to/user/templates'),257 ... PackageLoader('myapplication')258 .. ])258 ... ])259260 This is useful if you want to allow users to override builtin templates261 from a different location.262 """
11 unmodified lines
modified:jinja2/parser.py+2 added-2 removed
27 unmodified lines
28 self.source = unicode(source)29 self.name = name30 self.filename = filename31 self.closed = False32 self.stream = environment.lexer.tokenize(self.source, filename)32 self.stream = environment.lexer.tokenize(self.source, name, filename)33 self.extensions = {}34 for extension in environment.extensions.itervalues():35 for tag in extension.tags:36 self.extensions[tag] = extension.parse
5 unmodified lines
42 filename.43 """44 if lineno is None:45 lineno = self.stream.current.lineno46 raise TemplateSyntaxError(msg, lineno, self.name, self.filename)46 raise exc(msg, lineno, self.name, self.filename)4748 def is_tuple_end(self, extra_end_rules=None):49 """Are we at the end of a tuple?"""50 if self.stream.current.type in ('variable_end', 'block_end', 'rparen'):
705 unmodified lines
modified:jinja2/runtime.py-1 removed
7 unmodified lines
8 :copyright: Copyright 2008 by Armin Ronacher.9 :license: GNU GPL.10"""11import sys12from types import FunctionType12from itertools import chain, imap13from jinja2.utils import Markup, partial, soft_unicode, escape, missing, concat14from jinja2.exceptions import UndefinedError, TemplateRuntimeError15
430 unmodified lines