| Armin Ronacher | 94dd3d0 | 2007-03-14 18:43:57 +0100 | [diff] [blame] | 1 | ======================= |
| 2 | Context and Environment |
| 3 | ======================= |
| 4 | |
| 5 | The two central objects in Jinja are the `Environment` and `Context`. Both |
| 6 | are designed to be subclassed by applications if they need to extend Jinja. |
| 7 | |
| 8 | Environment |
| 9 | =========== |
| 10 | |
| 11 | The initialization parameters are already covered in the `Quickstart`_ thus |
| 12 | not repeated here. |
| 13 | |
| 14 | But beside those configurable instance variables there are some functions used |
| 15 | in 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 Ronacher | e9e1788 | 2007-03-19 17:52:24 +0100 | [diff] [blame] | 36 | 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 Ronacher | 94dd3d0 | 2007-03-14 18:43:57 +0100 | [diff] [blame] | 43 | **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 Ronacher | ae16fd0 | 2007-03-27 21:31:24 +0200 | [diff] [blame] | 60 | **Warning** this is a jinja internal method. The actual implementation |
| 61 | and function signature might change. |
| 62 | |
| Armin Ronacher | 94dd3d0 | 2007-03-14 18:43:57 +0100 | [diff] [blame] | 63 | **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 Ronacher | ae16fd0 | 2007-03-27 21:31:24 +0200 | [diff] [blame] | 75 | **Warning** this is a jinja internal method. The actual implementation |
| 76 | and function signature might change. |
| Armin Ronacher | 94dd3d0 | 2007-03-14 18:43:57 +0100 | [diff] [blame] | 77 | |
| Armin Ronacher | ae16fd0 | 2007-03-27 21:31:24 +0200 | [diff] [blame] | 78 | **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 Ronacher | 94dd3d0 | 2007-03-14 18:43:57 +0100 | [diff] [blame] | 85 | |
| 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 Ronacher | ae16fd0 | 2007-03-27 21:31:24 +0200 | [diff] [blame] | 95 | **Warning** this is a jinja internal method. The actual implementation |
| 96 | and function signature might change. |
| 97 | |
| Armin Ronacher | 94dd3d0 | 2007-03-14 18:43:57 +0100 | [diff] [blame] | 98 | **def** `call_function_simple` *(self, f, context)*: |
| 99 | |
| 100 | Like `call_function` but without arguments. |
| 101 | |
| Armin Ronacher | ae16fd0 | 2007-03-27 21:31:24 +0200 | [diff] [blame] | 102 | **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 Ronacher | 94dd3d0 | 2007-03-14 18:43:57 +0100 | [diff] [blame] | 106 | |
| 107 | Postprocess a variable before it's sent to the template. |
| Armin Ronacher | ae16fd0 | 2007-03-27 21:31:24 +0200 | [diff] [blame] | 108 | |
| 109 | **Warning** this is a jinja internal method. The actual implementation |
| 110 | and function signature might change. |
| Armin Ronacher | 94dd3d0 | 2007-03-14 18:43:57 +0100 | [diff] [blame] | 111 | |
| 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 | |
| 117 | Context |
| 118 | ======= |
| 119 | |
| 120 | Jinja wraps the variables passed to the template in a special class called a |
| 121 | context. This context supports variables on multiple layers and lazy (deferred) |
| 122 | objects. Often your application has a request object, database connection |
| 123 | object or something similar you want to access in filters, functions etc. |
| 124 | |
| 125 | Beacause of that you can easily subclass a context to add additional variables |
| 126 | or 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 Ronacher | e9e1788 | 2007-03-19 17:52:24 +0100 | [diff] [blame] | 160 | .. _i18n: i18n.txt |
| Armin Ronacher | 94dd3d0 | 2007-03-14 18:43:57 +0100 | [diff] [blame] | 161 | .. _Quickstart: devintro.txt |
| 162 | .. _gettext documentation: http://docs.python.org/lib/module-gettext.html |