Skip to content
Myr

objects.txt

This file

Path

docs/src/objects.txt

154 lines · 5.1 KiB

Last changed by

some documentation changesArmin Ronacherbefore adoption015b0c9

History of this file

Take it

The walk stopped after 200 commits: lines older than e98c5f5 point at it.

[svn] updated documentation regarding "{% call %}" and documented speedup module. also fixed some minor bugs in the speedup moduleArmin Ronacherbefore adoptione98c5f5

lines 1–7why, one step back
1==============
2Global objects3==============45This section covers the behavior of global objects in the Jinja namespace and6also the behavior of their attributes.7

some documentation changesArmin Ronacherbefore adoption015b0c9

line 8why, one step back
8

[svn] updated documentation regarding "{% call %}" and documented speedup module. also fixed some minor bugs in the speedup moduleArmin Ronacherbefore adoptione98c5f5

lines 9–52why, one step back
9Functions
10=========1112`Filters`_ and `Tests`_ are special kind of functions you cannot call without13and argument. Global functions are different. Those are really just ordinary14python functions. You can register them on the global namespace easily:1516.. sourcecode:: python1718 def my_function():19 return [1, 2, 3]2021 env.globals['my_function'] = my_function2223In the template you can not call it like this:2425.. sourcecode:: jinja2627 {{ my_function() }}2829Of couse you can pass any argument to it. But what if you want to access the30context or environment? In that case you have to decorate a function or mark31it (both work exactly the same, but the decorator way is probably nicer if you32have python >= 2.4):3334.. sourcecode:: python3536 from cgi import escape37 from pprint import pformat38 from jinja.datastructure import contextcallable3940 @contextcallable41 def printcontext(env, context):42 result = []43 for key, value in context.to_dict():44 result.append('<tr><th>%s</th><td>%r</td></tr>' %45 (escape(unicode(key)), escape(pformat(value))))46 return '<table>%s</table>' % u'\n'.join(result)4748If this function is registered in the environment namespace then and called49from the template it should return the content of the context as simple html50template. Of course you can modify the context too. For more informations about51the context object have a look at the `context object`_ documentation.52

[svn] added many new tests to jinjaArmin Ronacherbefore adoptionccf284b

lines 53–76why, one step back
53The new ``{% call %}`` tag that exists with Jinja 1.1 onwards can not only
54be used with Jinja macros but also with Python functions. If a template55designers uses the ``{% call %}`` tag to call a function provided by the56application Jinja will call this function with a keyword argument called57`caller` which points to a `function`. If you call this function (optionally58with keyword arguments that appear in the context) you get a string back59that was the content of that block. This should explain this:6061.. sourcecode:: python6263 def make_dialog(title, caller=None):64 body = ''65 if caller:66 body = caller(title=title)67 return '<div class="dialog"><h2>%s</h2>%s</div>' % (title, body)6869This can be used like this in the template now:7071.. sourcecode:: html+jinja7273 {% call make_dialog('Dialog Title') %}74 This is the body of the dialog entitled "{{ title }}".75 {% endcall %}76

[svn] updated documentation regarding "{% call %}" and documented speedup module. also fixed some minor bugs in the speedup moduleArmin Ronacherbefore adoptione98c5f5

lines 77–117why, one step back
77Deferred Values
78===============7980You can pass functions to the template that are called to provide values at81first access. Say you want to pass the list of recently written comments to82all templates. But not necessarily every template will render that. In that83situation you can create a function that returns that value and wrap it in an84`Deferred` object. The first time a template accesses the variable then the85`Deferred` object is resolved:8687.. sourcecode:: python8889 from jinja.datastructure import Deferred90 from yourapplication.models import Comments9192 def get_recent_comments(env, context, name):93 # do what ever you want here.94 return Comments.get_recent(10)9596 env.globals['recent_comments'] = Deferred(get_recent_comments)9798The function is always called with the same arguments. The first one is the99current environment, the second the context and the third is the name of the100variable. In this example ``recent_comments``.101102The value is cached until rendering/streaming finished.103104Unsafe Methods / Attributes105===========================106107Because Jinja is sandboxed it provides several ways to prevent unsafe attribute108access. You can mark both attributes and methods as unsafe:109110.. sourcecode:: python111112 from jinja.datastructure import unsafe113114 class MyModel(...):115116 # just give access to a and b. Default is all117 # note that this also disallows the functions from below.

[svn] splitted jinja documentation into more filesArmin Ronacherbefore adoption4c9cfcc

lines 118–119why, one step back
118 # if you use jinja_allowed_attributes you don't have add the
119 # code below since methods are treated as attributes too.

[svn] updated documentation regarding "{% call %}" and documented speedup module. also fixed some minor bugs in the speedup moduleArmin Ronacherbefore adoptione98c5f5

lines 120–154why, one step back
120 jinja_allowed_attributes = ['a', 'b']
121122 def __init__(self, ...):123 ...124 self.a = ...125 self.b = ...126127 # python2.4 way of marking methods128 @unsafe129 def save(self):130 """Save the model."""131132 # python2.3 way for the same133 def delete(self):134 """Delete the model."""135 delete.jinja_unsafe_call = True136137138Bypassing Automatic Filtering139=============================140141With Jinja 1.1 it's possible to assign filters to any variable printed. Of142course there are ways to bypass that in order to make sure an object does143not get escaped etc.144145In order to disable automatic filtering for an object you have to make146sure that it's either an subclass of `unicode` or implements a147`__unicode__` method. `__str__` will work too as long as the return value148only contains ASCII values. Additionally you have to add an attribute to149that object named `jinja_no_finalization` and set that to `True`.150151152.. _Filters: filters.txt153.. _Tests: tests.txt154.. _context object: contextenv.txt