blob: f362a64caf0fb0a1df0f854956e9baf75595d14c [file] [log] [blame]
Armin Ronacher94dd3d02007-03-14 18:43:57 +01001=======================
2Context and Environment
3=======================
4
5The two central objects in Jinja are the `Environment` and `Context`. Both
6are designed to be subclassed by applications if they need to extend Jinja.
7
8Environment
9===========
10
11The initialization parameters are already covered in the `Quickstart`_ thus
12not repeated here.
13
14But beside those configurable instance variables there are some functions used
15in the template evaluation code you may want to override:
16
17**def** `to_unicode` *(self, value)*:
18
19 Called to convert variables to unicode. Per default this checks if the
20 value is already unicode. If not it's converted to unicode using the
21 charset defined on the environment.
22
23 Also `None` is converted into an empty string per default.
24
25**def** `get_translator` *(self, context)*:
26
27 Return the translator used for i18n. A translator is an object that
28 provides the two functions ``gettext(string)`` and
29 ``ngettext(singular, plural, n)``. Both of those functions have to
30 behave like the `ugettext` and `nugettext` functions described in the
31 python `gettext documentation`_.
32
33 If you don't provide a translator a default one is used to switch
34 between singular and plural forms.
35
Armin Ronachere9e17882007-03-19 17:52:24 +010036 Have a look at the `i18n`_ section for more information.
37
38**def** `get_translations` *(self, name)*:
39
40 Get the translations for the template `name`. Only works if a loader
41 is present. See the `i18n`_ section for more details.
42
Armin Ronacher94dd3d02007-03-14 18:43:57 +010043**def** `apply_filters` *(self, value, context, filters)*:
44
45 Now this function is a bit tricky and you usually don't have to override
46 it. It's used to apply filters on a value. The Jinja expression
47 ``{{ foo|escape|replace('a', 'b') }}`` calls the function with the
48 value of `foo` as first parameter, the current context as second and
49 a list of filters as third. The list looks like this:
50
51 .. sourcecode:: python
52
53 [('escape', ()), ('replace', (u'a', u'b'))]
54
55 As you can see the filter `escape` is called without arguments whereas
56 `replace` is called with the two literal strings ``a`` and ``b``, both
57 unicode. The filters for the names are stored on ``self.filters`` in a
58 dict. Missing filters should raise a `FilterNotFound` exception.
59
Armin Ronacherae16fd02007-03-27 21:31:24 +020060 **Warning** this is a jinja internal method. The actual implementation
61 and function signature might change.
62
Armin Ronacher94dd3d02007-03-14 18:43:57 +010063**def** `perform_test` *(self, context, testname, args, value, invert)*:
64
65 Like `apply_filters` you usually don't override this one. It's the
66 callback function for tests (``foo is bar`` / ``foo is not bar``).
67
68 The first parameter is the current contex, the second the name of
69 the test to perform. the third a tuple of arguments, the fourth is
70 the value to test. The last one is `True` if the test was performed
71 with the `is not` operator, `False` if with the `is` operator.
72
73 Missing tests should raise a `TestNotFound` exception.
74
Armin Ronacherae16fd02007-03-27 21:31:24 +020075 **Warning** this is a jinja internal method. The actual implementation
76 and function signature might change.
Armin Ronacher94dd3d02007-03-14 18:43:57 +010077
Armin Ronacherae16fd02007-03-27 21:31:24 +020078**def** `get_attribute` *(self, obj, attribute)*:
79
80 Get `attribute` from the object provided. The default implementation
81 performs security tests.
82
83 **Warning** this is a jinja internal method. The actual implementation
84 and function signature might change.
Armin Ronacher94dd3d02007-03-14 18:43:57 +010085
86**def** `call_function` *(self, f, context, args, kwargs, dyn_args, dyn_kwargs)*:
87
88 Call a function `f` with the arguments `args`, `kwargs`, `dyn_args` and
89 `dyn_kwargs` where `args` is a tuple and `kwargs` a dict. If `dyn_args`
90 is not `None` you have to add it to the arguments, if `dyn_kwargs` is
91 not `None` you have to update the `kwargs` with it.
92
93 The default implementation performs some security checks.
94
Armin Ronacherae16fd02007-03-27 21:31:24 +020095 **Warning** this is a jinja internal method. The actual implementation
96 and function signature might change.
97
Armin Ronacher94dd3d02007-03-14 18:43:57 +010098**def** `call_function_simple` *(self, f, context)*:
99
100 Like `call_function` but without arguments.
101
Armin Ronacherae16fd02007-03-27 21:31:24 +0200102 **Warning** this is a jinja internal method. The actual implementation
103 and function signature might change.
104
105**def** `finish_var` *(self, value, ctx)*:
Armin Ronacher94dd3d02007-03-14 18:43:57 +0100106
107 Postprocess a variable before it's sent to the template.
Armin Ronacherae16fd02007-03-27 21:31:24 +0200108
109 **Warning** this is a jinja internal method. The actual implementation
110 and function signature might change.
Armin Ronacher94dd3d02007-03-14 18:43:57 +0100111
112.. admonition:: Note
113
114 The Enviornment class is defined in `jinja.environment.Environment`
115 but imported into the `jinja` package because it's often used.
116
117Context
118=======
119
120Jinja wraps the variables passed to the template in a special class called a
121context. This context supports variables on multiple layers and lazy (deferred)
122objects. Often your application has a request object, database connection
123object or something similar you want to access in filters, functions etc.
124
125Beacause of that you can easily subclass a context to add additional variables
126or to change the way it behaves.
127
128**def** `pop` *(self)*:
129
130 Pop the outermost layer and return it.
131
132**def** `push` *(self, data=None)*:
133
134 Push a dict to the stack or an empty layer.
135
136 Has to return the pushed object.
137
138**def** `to_dict` *(self)*:
139
140 Flatten the context and convert it into a dict.
141
142**def** `__getitem__` *(self, name)*:
143
144 Resolve an item. Per default this also resolves `Deferred` objects.
145
146**def** `__setitem__` *(self, name, value)*:
147
148 Set an item in the outermost layer.
149
150**def** `__delitem__` *(self, name)*:
151
152 Delete an item in the outermost layer. Do not raise exceptions if
153 the value does not exist.
154
155**def** `__contains__` *(self, name)*:
156
157 Return `True` if `name` exists in the context.
158
159
Armin Ronachere9e17882007-03-19 17:52:24 +0100160.. _i18n: i18n.txt
Armin Ronacher94dd3d02007-03-14 18:43:57 +0100161.. _Quickstart: devintro.txt
162.. _gettext documentation: http://docs.python.org/lib/module-gettext.html