Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame^] | 1 | API |
| 2 | === |
| 3 | |
| 4 | .. module:: jinja2 |
| 5 | :synopsis: public Jinja2 API |
| 6 | |
| 7 | This document describes the API to Jinja2 and not the template language. It |
| 8 | will be most useful as reference to those implementing the template interface |
| 9 | to the application and not those who are creating Jinja2 templates. |
| 10 | |
| 11 | Basics |
| 12 | ------ |
| 13 | |
| 14 | Jinja2 uses a central object called the template :class:`Environment`. |
| 15 | Instances of this class are used to store the configuration, global objects |
| 16 | and are used to load templates from the file system or other locations. |
| 17 | Even if you are creating templates from string by using the constructor of |
| 18 | :class:`Template` class, an environment is created automatically for you. |
| 19 | |
| 20 | Most applications will create one :class:`Environment` object on application |
| 21 | initialization and use that to load templates. In some cases it's however |
| 22 | useful to have multiple environments side by side, if different configurations |
| 23 | are in use. |
| 24 | |
| 25 | The simplest way to configure Jinja2 to load templates for your application |
| 26 | looks roughly like this:: |
| 27 | |
| 28 | from jinja2 import Environment, PackageLoader |
| 29 | env = Environment(loader=PackageLoader('yourapplication', 'templates')) |
| 30 | |
| 31 | This will create a template environment with the default settings and a |
| 32 | loader that looks up the templates in the `templates` folder inside the |
| 33 | `yourapplication` python package. Different loaders are available |
| 34 | and you can also write your own if you want to load templates from a |
| 35 | database or other resources. |
| 36 | |
| 37 | To load a template from this environment you just have to call the |
| 38 | :meth:`get_template` method which then returns the loaded :class:`Template`:: |
| 39 | |
| 40 | template = env.get_template('mytemplate.html') |
| 41 | |
| 42 | To render it with some variables, just call the :meth:`render` method:: |
| 43 | |
| 44 | print template.render(the='variables', go='here') |
| 45 | |
| 46 | |
| 47 | High Level API |
| 48 | -------------- |
| 49 | |
| 50 | .. autoclass:: jinja2.environment.Environment |
| 51 | :members: from_string, get_template, join_path |
| 52 | |
| 53 | .. attribute:: shared |
| 54 | |
| 55 | If a template was created by using the :class:`Template` constructor |
| 56 | an environment is created automatically. These environments are |
| 57 | created as shared environments which means that multiple templates |
| 58 | may have the same anonymous environment. For all shared environments |
| 59 | this attribute is `True`, else `False`. |
| 60 | |
| 61 | .. attribute:: sandboxed |
| 62 | |
| 63 | If the environment is sandboxed this attribute is `True`. For the |
| 64 | sandbox mode have a look at the documentation for the |
| 65 | :class:`~jinja2.sandbox.SandboxedEnvironment`. |
| 66 | |
| 67 | .. attribute:: filters |
| 68 | |
| 69 | A dict of filters for this environment. As long as no template was |
| 70 | loaded it's safe to add new filters or remove old. |
| 71 | |
| 72 | .. attribute:: tests |
| 73 | |
| 74 | A dict of test funcitons for this environment. As long as no |
| 75 | template way loaded it's safe to modify this dict. |
| 76 | |
| 77 | .. attribute:: globals |
| 78 | |
| 79 | A dict of global variables. These variables are always available |
| 80 | in a template and (if the optimizer is enabled) may not be |
| 81 | override by templates. As long as no template was loaded it's safe |
| 82 | to modify this dict. |
| 83 | |
| 84 | |
| 85 | .. autoclass:: jinja2.Template |
| 86 | :members: render, stream, generate, include |
| 87 | |
| 88 | |
| 89 | .. autoclass:: jinja2.environment.TemplateStream |
| 90 | :members: disable_buffering, enable_buffering |
| 91 | |
| 92 | |
| 93 | Undefined Types |
| 94 | --------------- |
| 95 | |
| 96 | These classes can be used as undefined types. The :class:`Environment` |
| 97 | constructor takes an `undefined` parameter that can be one of those classes |
| 98 | or a custom subclass of :class:`Undefined`. Whenever the template engine is |
| 99 | unable to look up a name or access an attribute one of those objects is |
| 100 | created and returned. Some operations on undefined values are then allowed, |
| 101 | others fail. |
| 102 | |
| 103 | The closest to regular Python behavior is the `StrictUndefined` which |
| 104 | disallows all operations beside testing if it's an undefined object. |
| 105 | |
| 106 | .. autoclass:: jinja2.runtime.Undefined |
| 107 | |
| 108 | .. autoclass:: jinja2.runtime.DebugUndefined |
| 109 | |
| 110 | .. autoclass:: jinja2.runtime.StrictUndefined |
| 111 | |
| 112 | |
| 113 | Loaders |
| 114 | ------- |
| 115 | |
| 116 | Loaders are responsible for loading templates from a resource such as the |
| 117 | file system and for keeping the compiled modules in memory. These work like |
| 118 | Python's `sys.modules` which keeps the imported templates in memory. Unlike |
| 119 | `sys.modules` however this cache is limited in size by default and templates |
| 120 | are automatically reloaded. Each loader that extends :class:`BaseLoader` |
| 121 | supports this caching and accepts two parameters to configure it: |
| 122 | |
| 123 | `cache_size` |
| 124 | The size of the cache. Per default this is ``50`` which means that if |
| 125 | more than 50 templates are loaded the loader will clean out the least |
| 126 | recently used template. If the cache size is set to ``0`` templates are |
| 127 | recompiled all the time, if the cache size is ``-1`` the cache will not |
| 128 | be cleaned. |
| 129 | |
| 130 | `auto_reload` |
| 131 | Some loaders load templates from locations where the template sources |
| 132 | may change (ie: file system or database). If `auto_reload` is set to |
| 133 | `True` (default) every time a template is requested the loader checks |
| 134 | if the source changed and if yes, it will reload the template. For |
| 135 | higher performance it's possible to disable that. |
| 136 | |
| 137 | .. autoclass:: jinja2.loaders.FileSystemLoader |
| 138 | |
| 139 | .. autoclass:: jinja2.loaders.PackageLoader |
| 140 | |
| 141 | .. autoclass:: jinja2.loaders.DictLoader |
| 142 | |
| 143 | .. autoclass:: jinja2.loaders.FunctionLoader |
| 144 | |
| 145 | .. autoclass:: jinja2.loaders.PrefixLoader |
| 146 | |
| 147 | .. autoclass:: jinja2.loaders.ChoiceLoader |
| 148 | |
| 149 | All loaders are subclasses of :class:`BaseLoader`. If you want to create your |
| 150 | own loader, subclass :class:`BaseLoader` and override `get_source`. |
| 151 | |
| 152 | .. autoclass:: jinja2.loaders.BaseLoader |
| 153 | :members: get_source, load |
| 154 | |
| 155 | |
| 156 | Utilities |
| 157 | --------- |
| 158 | |
| 159 | These helper functions and classes are useful if you add custom filters or |
| 160 | functions to a Jinja2 environment. |
| 161 | |
| 162 | .. autofunction:: jinja2.filters.environmentfilter |
| 163 | |
| 164 | .. autofunction:: jinja2.filters.contextfilter |
| 165 | |
| 166 | .. autofunction:: jinja2.utils.environmentfunction |
| 167 | |
| 168 | .. autofunction:: jinja2.utils.contextfunction |
| 169 | |
| 170 | .. function:: escape(s) |
| 171 | |
| 172 | Convert the characters &, <, >, and " in string s to HTML-safe sequences. |
| 173 | Use this if you need to display text that might contain such characters |
| 174 | in HTML. This function will not escaped objects that do have an HTML |
| 175 | representation such as already escaped data. |
| 176 | |
| 177 | .. autoclass:: jinja2.utils.Markup |
| 178 | |
| 179 | |
| 180 | Exceptions |
| 181 | ---------- |
| 182 | |
| 183 | .. autoclass:: jinja2.exceptions.TemplateError |
| 184 | |
| 185 | .. autoclass:: jinja2.exceptions.UndefinedError |
| 186 | |
| 187 | .. autoclass:: jinja2.exceptions.TemplateNotFound |
| 188 | |
| 189 | .. autoclass:: jinja2.exceptions.TemplateSyntaxError |
| 190 | |
| 191 | .. autoclass:: jinja2.exceptions.TemplateAssertionError |