some more doc changes in jinja for the upcoming release
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 c9fe16e · Browse files · 12 files·+196 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
12 files+196 added−20 removed
- tests:+16 added−0 removed
- other:+180 added−20 removed
Provenance
- Commit
825e522- Tree
c37e434- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
c9fe16e
12 files changed+196 added−20 removed
modified:docs/src/debugging.txt+12 added-7 removed
6 unmodified lines7 does not support the `__loader__` hook which is used by Jinja to provide8 templates. Although the import system was implemented three Python versions9 ago the default traceback system still doesn't support it.10 11-However most of the extended web development traceback module support it:11+However most of the extended web development traceback modules have support12+for `__loader__`. Either directly or via the linecache module:13 13-- `Colubrid Debugging Middleware`_14+- `Werkzeug Debugging Middleware`_15+- `Django`_16 - `cgitb`_17 - `EvalException`_18 17-Note that the django traceback module currently does not provide this. `A18-ticket`_ was filed already so there is hope that this will be fixed.19-19 To enable debugging you have to use one of those debugging systems or20 implement your own one with support for `__loader__`.21 22+The python implementation of the jinja debugger can only translate the first23+exception in a traceback which can lead to problematic results. If you have24+the extended debugger c extension compiled (default on unix systems but only25+available on Windows if you have a VisualStudio 2003 installation) all frames26+in the traceback will point to the correct linenumbers of the templates.27 24-.. _Colubrid Debugging Middleware: http://trac.pocoo.org/repos/colubrid/trunk/colubrid/debug.py28+29+.. _Werkzeug Debugging Middleware: http://werkzeug.pocoo.org/30+.. _Django: http://www.djangoproject.com/31 .. _cgitb: http://docs.python.org/lib/module-cgitb.html32 .. _EvalException: http://pythonpaste.org/module-paste.evalexception.html27-.. _A ticket: http://code.djangoproject.com/ticket/3734modified:docs/src/designerdoc.txt+8 added
221 unmodified lines222 ``none`` corresponds to `None` in python.223 ``undefined`` special Jinja undefined singleton.224 ======================= ===================================================225 226+.. admonition:: Common Pitfalls227+228+ Keep in mind that Jinja literals, keywords and functions are all lowercase.229+ If you're used to Python you probably tried to write `True` which evaluates230+ to undefined because it simply does not exist. The correct name for the231+ true value is just `true` (lowercase).232+ 233+234 Operators235 =========236 237 Inside ``{{ variable }}`` blocks, `if` conditions and many other parts you can328 unmodified linesmodified:docs/src/devrecipies.txt+12 added
36 unmodified lines37 return env.autorender('foo.html')38 39 In the template you can now access the local variables `seq` and `foo`.40 41+42 Using Django Filters with Jinja43 ===============================44 45 If you use Jinja in django and want to use some of the filters that29 unmodified lines75 76 Also keep in mind that Jinja knows about keywords, thus you cannot have a filter77 that is called `pluralize` for example.78 79+Such a conversion function is also part of the djangosupport module which was80+introduced in Jinja 1.2. It's explained on the `Framework Integration`_ page.81 82+83 Using Jinja in Django84 =====================85 86+.. admonition:: Note87+88+ This section is more or less obsolete as of Jinja 1.2 which provides a89+ djangosupport module, covered on the `Framework Integration`_ page.90+91 This snippet was contributed by Bryan McLemore. It provides a `render_to_response`92 function similar to the one shipped with django just that it uses Jinja for93 rendering. It applies the context processors on the context and consumes a94 `RequestContext`:38 unmodified lines133 return DjangoTranslator()134 135 Because Django uses gettext internally we can create just assign the136 ngettext and gettext functions directly to the translator class.137+138+139+.. _Framework Integration: frameworks.txtmodified:docs/src/escaping.txt+2 added-2 removed
38 unmodified lines39 processing).40 41 Starting with `Jinja 1.1` you can also handle some whitespace rules in the42 template. If you put an minus sign (`-`) to the start or end of an block43-(for example a `for` tag) or a comment, variable element you can remove44-the whitespaces after that block or before that block:43+(for example a `for` tag), a comment or variable element you can remove44+the whitespaces after or before that block:45 46 .. sourcecode:: jinja47 48 {% for item in seq -%}23 unmodified linesmodified:docs/src/frameworks.txt+108 added-1 removed
13 unmodified lines14 called `General Template Interface`_ which isn't implemented by any existing15 framework so far. This specification however tries to solve the problems that16 exist in Buffet.17 18+19 Buffet20 ======21 22 The buffet specification proposes that templates are named in dotted names. That85 unmodified lines108 a folder in the ``yourapplication`` python package.109 110 The `downloads` action uses the pylons specific leading foreslash notation.111 112+113 General Template Interface114 ==========================115 116 Because nobody implemented this specification so far it's not documented here5 unmodified lines122 ======123 124 Using Jinja in django is straightforward because django has a pretty low125 level response interface. Just have a look at the `developer recipies`_,124-there are some examples for django.126+there are some examples for django. Starting with Jinja 1.2 there is also127+a contrib module that simplifies using Jinja in an unicode enabled django.128 129+Quickstart130+----------131 132+To get started execute the following code at the bottom of your settings.py133+or in some general application file such as urls.py or a central module. The134+only thing that matters is that it's executed right *after* the settings135+were set up and *before* `django.contrib.jinja` is imported::136+137+ from jinja.contrib import djangosupport138+ djangosupport.configure()139+140+What this does is setting up a Jinja environment for this django instance141+with loaders for `TEMPLATE_DIRS` etc. It also converts a couple of default142+django filters such as `date` and `timesince` which are not available in143+Jinja per default. If you want to change the list you can provide others144+by passing a list with filter import names as `convert_filters` keyword145+argument.146+147+All other keyword arguments are forwarded to the environment. If you want148+to provide a loader yourself pass it a loader keyword argument.149+150+Rendering Templates151+-------------------152+153+To render a template you can use the functions `render_to_string` or154+`render_to_response` from the `django.contrib.jinja` module::155+156+ from django.contrib.jinja import render_to_response157+ resp = render_to_response('Hello {{ username }}!', {158+ 'username': req.session['username']159+ }, req)160+161+`render_to_string` and `render_to_response` take at least the name of162+the template as argument, then the optional dict which will become the163+context. If you also provide a request object as third argument the164+context processors will be applied.165+166+`render_to_response` also takes a forth parameter which can be the167+content type which defaults to `DEFAULT_CONTENT_TYPE`.168+169+Converting Filters170+------------------171+172+One of the useful objects provided by `django.contrib.jinja` is the173+`register` object which can be used to register filters, tests and174+global objects. You can also convert any filter django provides in175+a Jinja filter using `convert_django_filter`::176+177+ from django.contrib.jinja import register, convert_django_filter178+ from django.template.defaultfilters import floatformat179+180+ register.filter(convert_django_filter(floatformat), 'floatformat')181+182+Available methods on the `register` object:183+184+``object (obj[, name])``185+ Register a new global as name or with the object's name.186+ Returns the function object unchanged so that you can use187+ it as decorator if no name is provided.188+189+``filter (func[, name])``190+ Register a function as filter with the name provided or191+ the object's name as filtername.192+ Returns the function object unchanged so that you can use193+ it as decorator if no name is provided.194+195+``test (func[, name])``196+ Register a function as test with the name provided or the197+ object's name as testname.198+ Returns the function object unchanged so that you can use199+ it as decorator if no name is provided.200+201+``context_inclusion (func, template[, name])``202+ Register a function with a name provided or the func object's203+ name in the global namespace that acts as subrender function.204+205+ func is called with the callers context as dict and the206+ arguments and keywords argument of the inclusion function.207+ The function should then process the context and return a208+ new context or the same context object. Afterwards the209+ template is rendered with this context.210+211+ Example::212+213+ def add_author(context, author=None):214+ if author is not None:215+ author = Author.objects.get(name=author)216+ context['author'] = author217+ return context218+219+ register.context_inclusion(add_author, 'author_details.html',220+ 'render_author_details')221+222+ You can use it in the template like this then::223+224+ {{ render_author_details('John Doe') }}225+226+``clean_inclusion (func, template[, name[, run_processors]]) ``227+ Works like `context_inclusion` but doesn't use the calles228+ context but an empty context. If `run_processors` is `True`229+ it will lookup the context for a `request` object and pass230+ it to the render function to apply context processors.231+232+233+234 .. _Pylons: http://www.pylonshq.com/235 .. _General Template Interface: http://trac.pocoo.org/wiki/GeneralTemplateInterface236 .. _plugin module: http://trac.pocoo.org/browser/jinja/trunk/jinja/plugin.py237 .. _developer recipies: devrecipies.txtmodified:docs/src/installation.txt+17 added-1 removed
2 unmodified lines3 ============4 5 Jinja requires at least Python 2.3 to work correctly.6 7+8 Installing a released version9 =============================10 11 As a Python egg (via easy_install)18 unmodified lines30 internet connection.31 32 This will install Jinja into your Python installation's site-packages directory.33 34+35 Installing the development version36 ==================================37 38 If you want to play around with the code4 unmodified lines43 3. ``cd jinja``44 4. ``ln -s jinja /usr/lib/python2.X/site-packages``45 46 As an alternative to steps 4 you can also do ``python setup.py develop``45-which will install the package via setuptools in development mode.47+which will install the package via setuptools in development mode. This also48+has the advantage that the c extensions are compiled.49 50 If you just want the latest features and use them51 -------------------------------------------------52 4 unmodified lines57 This will install a Jinja egg containing the latest Subversion trunk code58 in your Python installation's site-packages directory. Every time the command59 is run, the sources are updated from Subversion.60 61+62+Windows Installation Information63+================================64+65+On windows computers without Visual Studio 2003 Jinja is unable to compile the66+C extensions. This is usually not a problem because there is a fallback for67+native Python implementations of those C libraries. However under certain68+conditions the installation may fail with an error message. In that case try69+to disable the extensions by passing the following parameters to setup.py::70+71+ python setup.py install --without-speedups --without-extended-debugger72+73+74 Documentation75 =============76 77 The egg builds include a documentation which is available in the ``docs`` folder12 unmodified linesmodified:docs/src/scopes.txt+2 added-3 removed
180 unmodified lines181 {% endfor %}182 183 After the iteration `last_item` will point to the item of the last iteration.184 185-If `last_item` was not defined in the outer scope it wouldn't exist now because186-in that situation `set`, even with a postfixed bang just behaves like setting a187-scope variable.185+If `last_item` was not defined in the outer scope it would be defined in the186+outermost scope.modified:jinja/datastructure.py+1 added-1 removed
272 unmodified lines273 for layer in self.stack[:0:-1]:274 if name in layer:275 layer[name] = value276 return277- self.current[name] = value277+ self.initial[name] = value278 279 def translate_func(self):280 """281 The translation function for this context. It takes427 unmodified linesmodified:tests/test_security.py+16 added
4 unmodified lines5 6 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8 """9+from jinja import Environment10 11 12+NONLOCALSET = '''\13+{% for item in range(10) %}14+ {%- set outer = item! -%}15+{% endfor -%}16+{{ outer }}'''17+18+19 class PrivateStuff(object):20 bar = lambda self: 2321 foo = lambda self: 4222 foo.jinja_unsafe_call = True33 unmodified lines56 Traceback (most recent call last):57 ...58 TemplateSyntaxError: cannot assign to expression (line 1)59 '''60+61+62+def test_nonlocal_set():63+ env = Environment()64+ env.globals['outer'] = 4265+ tmpl = env.from_string(NONLOCALSET)66+ assert tmpl.render() == '9'67+ assert env.globals['outer'] == 42added:.hgignore+6 added
1+^instance$2+^instance/3+^jinja/.*\.so$4+^(build|dist|Jinja\.egg-info)/5+\.py[co]$6+\.DS_Store$modified:CHANGES+8 added-1 removed
1 unmodified line2 ===============3 4 Version 1.25 -----------6-(codename to be selected, release date unknown)6+(codename to be hatsuyuki, released Nov 17th 2007)7 8+.. admonition:: Backwards Incompatible Changes9+10+ - `call` is a keyword now11+ - the internal parser AST changed12+ - `extends` must be the first tag in a template13+ - the tuple literal yields tuples now, instead of lists.14+15 - environments now have a `translator_factory` parameter that allows16 to change the translator without subclassing the environment.17 18 - fixed bug in buffet plugin regarding the package loader199 unmodified linesmodified:setup.py+4 added-4 removed
88 unmodified lines89 extras_require={'plugin': ['setuptools>=0.6a2']},90 features={91 'speedups': Feature(92 'optional C-speed enhancements',93- standard = True,94- ext_modules = [93+ standard=True,94+ ext_modules=[95 Extension('jinja._speedups', ['jinja/_speedups.c'])96 ]97 ),98 'extended-debugger': Feature(99 'extended debugger',100- standard = True,101- ext_modules = [100+ standard=True,101+ ext_modules=[102 Extension('jinja._debugger', ['jinja/_debugger.c'])103 ]104 )105 },2 unmodified lines