Skip to content
Myr

objects.txt

This file

Path

docs/src/objects.txt

153 lines · 5.1 KiB

Last changed by

[svn] added many new tests to jinjaArmin Ronacherbefore adoptionccf284b

History of this file

Take it

[svn] again doc updatesArmin Ronacherbefore adoption94dd3d0

lines 1–51why, one step back
1==============
2Global objects3==============45This section covers the behavior of global objects in the Jinja namespace and6also the behavior of their attributes.78Functions9=========1011`Filters`_ and `Tests`_ are special kind of functions you cannot call without12and argument. Global functions are different. Those are really just ordinary13python functions. You can register them on the global namespace easily:1415.. sourcecode:: python1617 def my_function():18 return [1, 2, 3]1920 env.globals['my_function'] = my_function2122In the template you can not call it like this:2324.. sourcecode:: jinja2526 {{ my_function() }}2728Of couse you can pass any argument to it. But what if you want to access the29context or environment? In that case you have to decorate a function or mark30it (both work exactly the same, but the decorator way is probably nicer if you31have python >= 2.4):3233.. sourcecode:: python3435 from cgi import escape36 from pprint import pformat37 from jinja.datastructure import contextcallable3839 @contextcallable40 def printcontext(env, context):41 result = []42 for key, value in context.to_dict():43 result.append('<tr><th>%s</th><td>%r</td></tr>' %44 (escape(unicode(key)), escape(pformat(value))))45 return '<table>%s</table>' % u'\n'.join(result)4647If this function is registered in the environment namespace then and called48from the template it should return the content of the context as simple html49template. Of course you can modify the context too. For more informations about50the context object have a look at the `context object`_ documentation.51

[svn] added many new tests to jinjaArmin Ronacherbefore adoptionccf284b

lines 52–75why, one step back
52The new ``{% call %}`` tag that exists with Jinja 1.1 onwards can not only
53be used with Jinja macros but also with Python functions. If a template54designers uses the ``{% call %}`` tag to call a function provided by the55application Jinja will call this function with a keyword argument called56`caller` which points to a `function`. If you call this function (optionally57with keyword arguments that appear in the context) you get a string back58that was the content of that block. This should explain this:5960.. sourcecode:: python6162 def make_dialog(title, caller=None):63 body = ''64 if caller:65 body = caller(title=title)66 return '<div class="dialog"><h2>%s</h2>%s</div>' % (title, body)6768This can be used like this in the template now:6970.. sourcecode:: html+jinja7172 {% call make_dialog('Dialog Title') %}73 This is the body of the dialog entitled "{{ title }}".74 {% endcall %}75

[svn] again doc updatesArmin Ronacherbefore adoption94dd3d0

lines 76–100why, one step back
76Deferred Values
77===============7879You can pass functions to the template that are called to provide values at80first access. Say you want to pass the list of recently written comments to81all templates. But not necessarily every template will render that. In that82situation you can create a function that returns that value and wrap it in an83`Deferred` object. The first time a template accesses the variable then the84`Deferred` object is resolved:8586.. sourcecode:: python8788 from jinja.datastructure import Deferred89 from yourapplication.models import Comments9091 def get_recent_comments(env, context, name):92 # do what ever you want here.93 return Comments.get_recent(10)9495 env.globals['recent_comments'] = Deferred(get_recent_comments)9697The function is always called with the same arguments. The first one is the98current environment, the second the context and the third is the name of the99variable. In this example ``recent_comments``.100

[svn] added c implementation of the jinja context class.Armin Ronacherbefore adoptionee2c18e

lines 101–102why, one step back
101The value is cached until rendering/streaming finished.
102

[svn] again doc updatesArmin Ronacherbefore adoption94dd3d0

lines 103–115why, one step back
103Unsafe Methods / Attributes
104===========================105106Because Jinja is sandboxed it provides several ways to prevent unsafe attribute107access. You can mark both attributes and methods as unsafe:108109.. sourcecode:: python110111 from jinja.datastructure import unsafe112113 class MyModel(...):114115 # just give access to a and b. Default is all

[svn] improved jinja debugging system. now handles errors in included templates tooArmin Ronacherbefore adoption18b3d0b

line 116why, one step back
116 # note that this also disallows the functions from below.

[svn] splitted jinja documentation into more filesArmin Ronacherbefore adoption4c9cfcc

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

[svn] again doc updatesArmin Ronacherbefore adoption94dd3d0

lines 119–136why, one step back
119 jinja_allowed_attributes = ['a', 'b']
120121 def __init__(self, ...):122 ...123 self.a = ...124 self.b = ...125126 # python2.4 way of marking methods127 @unsafe128 def save(self):129 """Save the model."""130131 # python2.3 way for the same132 def delete(self):133 """Delete the model."""134 delete.jinja_unsafe_call = True135136

[svn] implemented and documented jinja streaming interfaceArmin Ronacherbefore adoption02d81f7

lines 137–150why, one step back
137Bypassing Automatic Filtering
138=============================139140With Jinja 1.1 it's possible to assign filters to any variable printed. Of141course there are ways to bypass that in order to make sure an object does142not get escaped etc.143144In order to disable automatic filtering for an object you have to make145sure that it's either an subclass of `unicode` or implements a146`__unicode__` method. `__str__` will work too as long as the return value147only contains ASCII values. Additionally you have to add an attribute to148that object named `jinja_no_finalization` and set that to `True`.149150

[svn] again doc updatesArmin Ronacherbefore adoption94dd3d0

lines 151–153why, one step back
151.. _Filters: filters.txt
152.. _Tests: tests.txt153.. _context object: contextenv.txt