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
Provenance
- Commit
61a5a24- Tree
82e77f6- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
0c4f757
7 files changed+63 added−17 removed
modified:docs/api.rst+42 added-2 removed
13 unmodified lines14 Jinja2 uses a central object called the template :class:`Environment`.15 Instances of this class are used to store the configuration, global objects16 and are used to load templates from the file system or other locations.17 Even 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,19+albeit a shared one.20 21 Most applications will create one :class:`Environment` object on application22 initialization and use that to load templates. In some cases it's however23 useful to have multiple environments side by side, if different configurations19 unmodified lines43 To render it with some variables, just call the :meth:`render` method::44 45 print template.render(the='variables', go='here')46 47+Using a template loader rather then passing strings to :class:`Template`48+or :meth:`Environment.from_string` has multiple advantages. Besides being49+a lot easier to use it also enables template inheritance.50 51+52 Unicode53 -------54 55 Jinja2 is using unicode internally which means that you have to pass unicode56 objects to the render function or bytestrings that only consist of ASCII57 characters. Additionally newlines are normalized to one end of line58 sequence which is per default UNIX style (``\n``).59 60+Python 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 type62+called `basestring`. Unfortunately the default is `str` which should not63+be used to store text based information unless only ASCII characters are64+used. With Python 2.6 it is possible to my `unicode` the default on a per65+module level and with Python 3 it will be the default.66 67+To explicitly use a unicode string you have to prefix the string literal68+with a `u`: ``u'Hänsel und Gretel sagen Hallo'``. That way Python will69+store the string as unicode by decoding the string with the character70+encoding from the current Python module. If no encoding is specified this71+defaults to 'ASCII' which means that you can't use any non ASCII identifier.72+73+To set a better module encoding add the following comment to the first or74+second line of the Python module using the unicode literal::75+76+ # -*- coding: utf-8 -*-77+78+We recommend utf-8 as Encoding for Python modules and templates as it's79+possible to represent every Unicode character in utf-8 and because it's80+backwards compatible to ASCII. For Jinja2 the default encoding of templates81+is assumed to be utf-8.82+83+It is not possible to use Jinja2 to process non unicode data. The reason84+for this is that Jinja2 uses Unicode already on the language level. For85+example Jinja2 treats the non-breaking space as valid whitespace inside86+expressions which requires knowledge of the encoding or operating on an87+unicode string.88+89+For more details about unicode in Python have a look at the excellent90+`Unicode documentation`_.91+92+93+.. _Unicode documentation: http://docs.python.org/dev/howto/unicode.html94+95 High Level API96 --------------97 98 .. autoclass:: Environment([options])387 unmodified lines486 -------------487 488 The low level API exposes functionality that can be useful to understand some489 implementation details, debugging purposes or advanced :ref:`extension451-<jinja-extensions>` techniques.490+<jinja-extensions>` techniques. Unless you know exactly what you are doing we491+don't recommend using any of those.492 493 .. automethod:: Environment.lex494 495 .. automethod:: Environment.parse25 unmodified linesmodified:docs/faq.rst+4 added-4 removed
19 unmodified lines20 benchmark different engines in different situations. The benchmarks from the21 testsuite show that Jinja2 has a similar performance to `Mako`_ and is between22 10 and 20 times faster than Django's template engine or Genshi. These numbers23 should be taken with tons of salt as the benchmarks that took these numbers24-only test a few performance related situations such as looping. They are25-not a good indicator for the templates used in the average application.26-Additionally you should keep in mind that for most web applications27-templates are clearly not the bottleneck.24+only test a few performance related situations such as looping. Generally25+speaking the performance of a template engine doesn't matter much as the26+usual bottleneck in a web application is either the database or the application27+code.28 29 .. _Mako: http://www.makotemplates.org/30 31 How Compatible is Jinja2 with Django?116 unmodified linesmodified:docs/intro.rst+9 added-3 removed
32 unmodified lines33 -------------34 35 Jinja2 needs at least **Python 2.4** to run. Additionally a working C-compiler36 that can create python extensions should be installed for the debugger. If no37-C-compiler is available the `ctypes`_ module should be installed.37+C-compiler is available and you are using Python 2.4 the `ctypes`_ module38+should be installed.39 40 .. _ctypes: http://python.net/crew/theller/ctypes/41 42 54 unmodified lines97 98 This section gives you a brief introduction to the Python API for Jinja2 templates.99 100 The 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,101-but the easiest101+:class:`Template`. This however is not the recommended way to work with it if102+your templates are not loaded from strings but the file system or another data103+source:104 105 >>> from jinja2 import Template106 >>> template = Template('Hello {{ name }}!')107 >>> template.render(name='John Doe')3 unmodified lines111 object that provides a method called :meth:`~Template.render` which when112 called with a dict or keyword arguments expands the template. The dict113 or keywords arguments passed to the template are the so-called "context"114 of the template.115+116+What you can see here is that Jinja2 is using unicode internally and the117+return value is an unicode string. So make sure that your application is118+indeed using unicode internally.modified:docs/templates.rst+5 added-4 removed
147 unmodified lines148 a list of numbers from ``1`` to ``9`` the output would be ``123456789``.149 150 Note that you must not use a whitespace between the tag and the minus sign:151 152- valid:152+ valid::153+154 {%- if foo -%}...{% endif %}155 155- invalid:156+ invalid::157 158 {% - if foo - %}...{% endif %}159 160 If :ref:`line-statements` are enabled they strip leading whitespace871 unmodified lines1032 For multiple placeholders always use keyword arguments to `format` as other1033 languages may not use the words in the same order.1034 1035 1035-do1036-~~1036+Expression Statement1037+~~~~~~~~~~~~~~~~~~~~1038 1039 If the expression-statement extension is loaded a tag called `do` is available1040 that works exactly like the regular variable expression (``{{ ... }}``) just1041 that it doesn't print anything. This can be used to modify lists::24 unmodified linesmodified:jinja2/loaders.py+1 added-1 removed
253 unmodified lines254 255 >>> loader = ChoiceLoader([256 ... FileSystemLoader('/path/to/user/templates'),257 ... PackageLoader('myapplication')258- .. ])258+ ... ])259 260 This is useful if you want to allow users to override builtin templates261 from a different location.262 """11 unmodified linesmodified:jinja2/parser.py+2 added-2 removed
27 unmodified lines28 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.parse5 unmodified lines42 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)47 48 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 linesmodified:jinja2/runtime.py-1 removed
7 unmodified lines8 :copyright: Copyright 2008 by Armin Ronacher.9 :license: GNU GPL.10 """11 import sys12-from types import FunctionType12 from itertools import chain, imap13 from jinja2.utils import Markup, partial, soft_unicode, escape, missing, concat14 from jinja2.exceptions import UndefinedError, TemplateRuntimeError15 430 unmodified lines