blob: 0dce61876d7b4706cd6e163f4afa89fbd473b205 [file] [log] [blame]
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +02001API
2===
3
4.. module:: jinja2
5 :synopsis: public Jinja2 API
6
7This document describes the API to Jinja2 and not the template language. It
8will be most useful as reference to those implementing the template interface
9to the application and not those who are creating Jinja2 templates.
10
11Basics
12------
13
14Jinja2 uses a central object called the template :class:`Environment`.
15Instances of this class are used to store the configuration, global objects
16and are used to load templates from the file system or other locations.
17Even if you are creating templates from string by using the constructor of
Armin Ronacher61a5a242008-05-26 12:07:44 +020018:class:`Template` class, an environment is created automatically for you,
19albeit a shared one.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020020
21Most applications will create one :class:`Environment` object on application
22initialization and use that to load templates. In some cases it's however
23useful to have multiple environments side by side, if different configurations
24are in use.
25
26The simplest way to configure Jinja2 to load templates for your application
27looks roughly like this::
28
29 from jinja2 import Environment, PackageLoader
30 env = Environment(loader=PackageLoader('yourapplication', 'templates'))
31
32This will create a template environment with the default settings and a
33loader that looks up the templates in the `templates` folder inside the
34`yourapplication` python package. Different loaders are available
35and you can also write your own if you want to load templates from a
36database or other resources.
37
38To load a template from this environment you just have to call the
39:meth:`get_template` method which then returns the loaded :class:`Template`::
40
41 template = env.get_template('mytemplate.html')
42
43To render it with some variables, just call the :meth:`render` method::
44
45 print template.render(the='variables', go='here')
46
Armin Ronacher61a5a242008-05-26 12:07:44 +020047Using a template loader rather then passing strings to :class:`Template`
48or :meth:`Environment.from_string` has multiple advantages. Besides being
49a lot easier to use it also enables template inheritance.
50
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020051
Armin Ronacherf3c35c42008-05-23 23:18:14 +020052Unicode
53-------
54
55Jinja2 is using unicode internally which means that you have to pass unicode
56objects to the render function or bytestrings that only consist of ASCII
57characters. Additionally newlines are normalized to one end of line
58sequence which is per default UNIX style (``\n``).
59
Armin Ronacher61a5a242008-05-26 12:07:44 +020060Python 2.x supports two ways of representing string objects. One is the
61`str` type and the other is the `unicode` type, both of which extend a type
62called `basestring`. Unfortunately the default is `str` which should not
63be used to store text based information unless only ASCII characters are
64used. With Python 2.6 it is possible to my `unicode` the default on a per
65module level and with Python 3 it will be the default.
66
67To explicitly use a unicode string you have to prefix the string literal
68with a `u`: ``u'Hรคnsel und Gretel sagen Hallo'``. That way Python will
69store the string as unicode by decoding the string with the character
70encoding from the current Python module. If no encoding is specified this
71defaults to 'ASCII' which means that you can't use any non ASCII identifier.
72
73To set a better module encoding add the following comment to the first or
74second line of the Python module using the unicode literal::
75
76 # -*- coding: utf-8 -*-
77
78We recommend utf-8 as Encoding for Python modules and templates as it's
79possible to represent every Unicode character in utf-8 and because it's
80backwards compatible to ASCII. For Jinja2 the default encoding of templates
81is assumed to be utf-8.
82
83It is not possible to use Jinja2 to process non unicode data. The reason
84for this is that Jinja2 uses Unicode already on the language level. For
85example Jinja2 treats the non-breaking space as valid whitespace inside
86expressions which requires knowledge of the encoding or operating on an
87unicode string.
88
89For more details about unicode in Python have a look at the excellent
90`Unicode documentation`_.
91
92
93.. _Unicode documentation: http://docs.python.org/dev/howto/unicode.html
Armin Ronacherf3c35c42008-05-23 23:18:14 +020094
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +020095High Level API
96--------------
97
Armin Ronacher9bb7e472008-05-28 11:26:59 +020098The high-level API is the API you will use in the application to load and
99render Jinja2 templates. The :ref:`low-level-api` on the other side is only
100useful if you want to dig deeper into Jinja2 or :ref:`develop extensions
101<jinja-extensions>`.
102
Armin Ronacher5411ce72008-05-25 11:36:22 +0200103.. autoclass:: Environment([options])
104 :members: from_string, get_template, join_path, extend
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200105
106 .. attribute:: shared
107
108 If a template was created by using the :class:`Template` constructor
109 an environment is created automatically. These environments are
110 created as shared environments which means that multiple templates
111 may have the same anonymous environment. For all shared environments
112 this attribute is `True`, else `False`.
113
114 .. attribute:: sandboxed
115
116 If the environment is sandboxed this attribute is `True`. For the
117 sandbox mode have a look at the documentation for the
118 :class:`~jinja2.sandbox.SandboxedEnvironment`.
119
120 .. attribute:: filters
121
122 A dict of filters for this environment. As long as no template was
Armin Ronacher7259c762008-04-30 13:03:59 +0200123 loaded it's safe to add new filters or remove old. For custom filters
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200124 see :ref:`writing-filters`. For valid filter names have a look at
125 :ref:`identifier-naming`.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200126
127 .. attribute:: tests
128
Lukas Meuserad48a2e2008-05-01 18:19:57 +0200129 A dict of test functions for this environment. As long as no
130 template was loaded it's safe to modify this dict. For custom tests
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200131 see :ref:`writing-tests`. For valid test names have a look at
132 :ref:`identifier-naming`.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200133
134 .. attribute:: globals
135
136 A dict of global variables. These variables are always available
Armin Ronacher981cbf62008-05-13 09:12:27 +0200137 in a template. As long as no template was loaded it's safe
Armin Ronacher7259c762008-04-30 13:03:59 +0200138 to modify this dict. For more details see :ref:`global-namespace`.
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200139 For valid object names have a look at :ref:`identifier-naming`.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200140
Armin Ronachered98cac2008-05-07 08:42:11 +0200141 .. automethod:: overlay([options])
142
Armin Ronacher5411ce72008-05-25 11:36:22 +0200143 .. method:: undefined([hint,] [obj,] name[, exc])
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200144
Armin Ronacher5411ce72008-05-25 11:36:22 +0200145 Creates a new :class:`Undefined` object for `name`. This is useful
146 for filters or functions that may return undefined objects for
147 some operations. All parameters except of `hint` should be provided
148 as keyword parameters for better readability. The `hint` is used as
149 error message for the exception if provided, otherwise the error
150 message generated from `obj` and `name` automatically. The exception
151 provided as `exc` is raised if something with the generated undefined
152 object is done that the undefined object does not allow. The default
153 exception is :exc:`UndefinedError`. If a `hint` is provided the
154 `name` may be ommited.
155
156 The most common way to create an undefined object is by providing
157 a name only::
158
159 return environment.undefined(name='some_name')
160
161 This means that the name `some_name` is not defined. If the name
162 was from an attribute of an object it makes sense to tell the
163 undefined object the holder object to improve the error message::
164
165 if not hasattr(obj, 'attr'):
166 return environment.undefined(obj=obj, name='attr')
167
168 For a more complex example you can provide a hint. For example
169 the :func:`first` filter creates an undefined object that way::
170
171 return environment.undefined('no first item, sequence was empty')
172
173 If it the `name` or `obj` is known (for example because an attribute
174 was accessed) it shold be passed to the undefined object, even if
175 a custom `hint` is provided. This gives undefined objects the
176 possibility to enhance the error message.
177
178.. autoclass:: Template
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200179 :members: module, make_module
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200180
Armin Ronacher7259c762008-04-30 13:03:59 +0200181 .. attribute:: globals
182
Armin Ronachered98cac2008-05-07 08:42:11 +0200183 The dict with the globals of that template. It's unsafe to modify
184 this dict as it may be shared with other templates or the environment
185 that loaded the template.
Armin Ronacher7259c762008-04-30 13:03:59 +0200186
187 .. attribute:: name
188
Armin Ronachered98cac2008-05-07 08:42:11 +0200189 The loading name of the template. If the template was loaded from a
190 string this is `None`.
191
Armin Ronacher5411ce72008-05-25 11:36:22 +0200192 .. attribute:: filename
193
194 The filename of the template on the file system if it was loaded from
195 there. Otherwise this is `None`.
196
Armin Ronachered98cac2008-05-07 08:42:11 +0200197 .. automethod:: render([context])
198
199 .. automethod:: generate([context])
200
201 .. automethod:: stream([context])
Armin Ronacher7259c762008-04-30 13:03:59 +0200202
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200203
Armin Ronacher6df604e2008-05-23 22:18:38 +0200204.. autoclass:: jinja2.environment.TemplateStream()
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200205 :members: disable_buffering, enable_buffering
206
207
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200208.. _identifier-naming:
209
210Notes on Identifiers
Armin Ronacher5411ce72008-05-25 11:36:22 +0200211--------------------
Armin Ronacherd1ff8582008-05-11 00:30:43 +0200212
213Jinja2 uses the regular Python 2.x naming rules. Valid identifiers have to
214match ``[a-zA-Z_][a-zA-Z0-9_]*``. As a matter of fact non ASCII characters
215are currently not allowed. This limitation will probably go away as soon as
216unicode identifiers are fully specified for Python 3.
217
218Filters and tests are looked up in separate namespaces and have slightly
219modified identifier syntax. Filters and tests may contain dots to group
220filters and tests by topic. For example it's perfectly valid to add a
221function into the filter dict and call it `to.unicode`. The regular
222expression for filter and test identifiers is
223``[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]*)*```.
224
225
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200226Undefined Types
227---------------
228
229These classes can be used as undefined types. The :class:`Environment`
230constructor takes an `undefined` parameter that can be one of those classes
231or a custom subclass of :class:`Undefined`. Whenever the template engine is
232unable to look up a name or access an attribute one of those objects is
233created and returned. Some operations on undefined values are then allowed,
234others fail.
235
236The closest to regular Python behavior is the `StrictUndefined` which
237disallows all operations beside testing if it's an undefined object.
238
Armin Ronacher5411ce72008-05-25 11:36:22 +0200239.. autoclass:: jinja2.runtime.Undefined()
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200240
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200241 .. attribute:: _undefined_hint
242
243 Either `None` or an unicode string with the error message for
244 the undefined object.
245
246 .. attribute:: _undefined_obj
247
248 Either `None` or the owner object that caused the undefined object
249 to be created (for example because an attribute does not exist).
250
251 .. attribute:: _undefined_name
252
253 The name for the undefined variable / attribute or just `None`
254 if no such information exists.
255
256 .. attribute:: _undefined_exception
257
258 The exception that the undefined object wants to raise. This
259 is usually one of :exc:`UndefinedError` or :exc:`SecurityError`.
260
261 .. method:: _fail_with_undefined_error(\*args, \**kwargs)
262
263 When called with any arguments this method raises
264 :attr:`_undefined_exception` with an error message generated
265 from the undefined hints stored on the undefined object.
266
Armin Ronacher5411ce72008-05-25 11:36:22 +0200267.. autoclass:: jinja2.runtime.DebugUndefined()
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200268
Armin Ronacher5411ce72008-05-25 11:36:22 +0200269.. autoclass:: jinja2.runtime.StrictUndefined()
270
271Undefined objects are created by calling :attr:`undefined`.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200272
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200273.. admonition:: Implementation
274
275 :class:`Undefined` objects are implemented by overriding the special
276 `__underscore__` methods. For example the default :class:`Undefined`
277 class implements `__unicode__` in a way that it returns an empty
278 string, however `__int__` and others still fail with an exception. To
279 allow conversion to int by returning ``0`` you can implement your own::
280
281 class NullUndefined(Undefined):
282 def __int__(self):
283 return 0
284 def __float__(self):
285 return 0.0
286
287 To disallow a method, just override it and raise
288 :attr:`_undefined_exception`. Because this is a very common idom in
289 undefined objects there is the helper method
290 :meth:`_fail_with_undefined_error`. That does that automatically. Here
291 a class that works like the regular :class:`UndefinedError` but chokes
292 on iteration::
293
294 class NonIterableUndefined(Undefined):
295 __iter__ = Undefined._fail_with_undefined_error
296
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200297
Armin Ronacher7259c762008-04-30 13:03:59 +0200298The Context
299-----------
300
Armin Ronacher6df604e2008-05-23 22:18:38 +0200301.. autoclass:: jinja2.runtime.Context()
Armin Ronacherf35e2812008-05-06 16:04:10 +0200302 :members: resolve, get_exported, get_all
Armin Ronacher7259c762008-04-30 13:03:59 +0200303
304 .. attribute:: parent
305
306 A dict of read only, global variables the template looks up. These
Armin Ronacher19cf9c22008-05-01 12:49:53 +0200307 can either come from another :class:`Context`, from the
Armin Ronacher5411ce72008-05-25 11:36:22 +0200308 :attr:`Environment.globals` or :attr:`Template.globals` or points
309 to a dict created by combining the globals with the variables
310 passed to the render function. It must not be altered.
Armin Ronacher7259c762008-04-30 13:03:59 +0200311
312 .. attribute:: vars
313
314 The template local variables. This list contains environment and
315 context functions from the :attr:`parent` scope as well as local
316 modifications and exported variables from the template. The template
317 will modify this dict during template evaluation but filters and
318 context functions are not allowed to modify it.
319
320 .. attribute:: environment
321
322 The environment that loaded the template.
323
324 .. attribute:: exported_vars
325
326 This set contains all the names the template exports. The values for
327 the names are in the :attr:`vars` dict. In order to get a copy of the
328 exported variables as dict, :meth:`get_exported` can be used.
329
330 .. attribute:: name
331
332 The load name of the template owning this context.
333
334 .. attribute:: blocks
335
336 A dict with the current mapping of blocks in the template. The keys
337 in this dict are the names of the blocks, and the values a list of
338 blocks registered. The last item in each list is the current active
339 block (latest in the inheritance chain).
340
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200341 .. automethod:: jinja2.runtime.Context.call(callable, \*args, \**kwargs)
342
343
344.. admonition:: Implementation
345
346 Context is immutable for the same reason Python's frame locals are
347 immutable inside functions. Both Jinja2 and Python are not using the
348 context / frame locals as data storage for variables but only as primary
349 data source.
350
351 When a template accesses a variable the template does not define, Jinja2
352 looks up the variable in the context, after that the variable is treated
353 as if it was defined in the template.
354
Armin Ronacher7259c762008-04-30 13:03:59 +0200355
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +0200356.. _loaders:
357
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200358Loaders
359-------
360
361Loaders are responsible for loading templates from a resource such as the
Armin Ronacher7259c762008-04-30 13:03:59 +0200362file system. The environment will keep the compiled modules in memory like
363Python's `sys.modules`. Unlike `sys.modules` however this cache is limited in
364size by default and templates are automatically reloaded.
Armin Ronachercda43df2008-05-03 17:10:05 +0200365All loaders are subclasses of :class:`BaseLoader`. If you want to create your
Armin Ronachercda43df2008-05-03 17:10:05 +0200366own loader, subclass :class:`BaseLoader` and override `get_source`.
367
368.. autoclass:: jinja2.loaders.BaseLoader
369 :members: get_source, load
370
371Here a list of the builtin loaders Jinja2 provides:
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200372
373.. autoclass:: jinja2.loaders.FileSystemLoader
374
375.. autoclass:: jinja2.loaders.PackageLoader
376
377.. autoclass:: jinja2.loaders.DictLoader
378
379.. autoclass:: jinja2.loaders.FunctionLoader
380
381.. autoclass:: jinja2.loaders.PrefixLoader
382
383.. autoclass:: jinja2.loaders.ChoiceLoader
384
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200385
386Utilities
387---------
388
389These helper functions and classes are useful if you add custom filters or
390functions to a Jinja2 environment.
391
392.. autofunction:: jinja2.filters.environmentfilter
393
394.. autofunction:: jinja2.filters.contextfilter
395
396.. autofunction:: jinja2.utils.environmentfunction
397
398.. autofunction:: jinja2.utils.contextfunction
399
400.. function:: escape(s)
401
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200402 Convert the characters ``&``, ``<``, ``>``, ``'``, and ``"`` in string `s`
403 to HTML-safe sequences. Use this if you need to display text that might
404 contain such characters in HTML. This function will not escaped objects
405 that do have an HTML representation such as already escaped data.
406
407 The return value is a :class:`Markup` string.
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200408
Armin Ronacher187bde12008-05-01 18:19:16 +0200409.. autofunction:: jinja2.utils.clear_caches
410
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200411.. autofunction:: jinja2.utils.is_undefined
412
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200413.. autoclass:: jinja2.utils.Markup
414
415
416Exceptions
417----------
418
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +0200419.. autoexception:: jinja2.exceptions.TemplateError
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200420
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +0200421.. autoexception:: jinja2.exceptions.UndefinedError
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200422
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +0200423.. autoexception:: jinja2.exceptions.TemplateNotFound
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200424
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +0200425.. autoexception:: jinja2.exceptions.TemplateSyntaxError
Armin Ronacher3c8b7ad2008-04-28 13:52:21 +0200426
Armin Ronacherf3c35c42008-05-23 23:18:14 +0200427 .. attribute:: message
428
429 The error message as utf-8 bytestring.
430
431 .. attribute:: lineno
432
433 The line number where the error occurred
434
435 .. attribute:: name
436
437 The load name for the template as unicode string.
438
439 .. attribute:: filename
440
441 The filename that loaded the template as bytestring in the encoding
442 of the file system (most likely utf-8 or mbcs on Windows systems).
443
444 The reason why the filename and error message are bytestrings and not
445 unicode strings is that Python 2.x is not using unicode for exceptions
446 and tracebacks as well as the compiler. This will change with Python 3.
447
Armin Ronacher5cdc1ac2008-05-07 12:17:18 +0200448.. autoexception:: jinja2.exceptions.TemplateAssertionError
Armin Ronacher7259c762008-04-30 13:03:59 +0200449
450
451.. _writing-filters:
452
453Custom Filters
454--------------
455
456Custom filters are just regular Python functions that take the left side of
457the filter as first argument and the the arguments passed to the filter as
458extra arguments or keyword arguments.
459
460For example in the filter ``{{ 42|myfilter(23) }}`` the function would be
461called with ``myfilter(42, 23)``. Here for example a simple filter that can
462be applied to datetime objects to format them::
463
464 def datetimeformat(value, format='%H:%M / %d-%m-%Y'):
465 return value.strftime(format)
466
467You can register it on the template environment by updating the
468:attr:`~Environment.filters` dict on the environment::
469
470 environment.filters['datetimeformat'] = datetimeformat
471
472Inside the template it can then be used as follows:
473
474.. sourcecode:: jinja
475
476 written on: {{ article.pub_date|datetimeformat }}
477 publication date: {{ article.pub_date|datetimeformat('%d-%m-%Y') }}
478
479Filters can also be passed the current template context or environment. This
480is useful if a filters wants to return an undefined value or check the current
481:attr:`~Environment.autoescape` setting. For this purpose two decorators
482exist: :func:`environmentfilter` and :func:`contextfilter`.
483
484Here a small example filter that breaks a text into HTML line breaks and
485paragraphs and marks the return value as safe HTML string if autoescaping is
486enabled::
487
488 import re
489 from jinja2 import environmentfilter, Markup, escape
490
491 _paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')
492
493 @environmentfilter
494 def nl2br(environment, value):
495 result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', '<br>\n')
496 for p in _paragraph_re.split(escape(value)))
497 if environment.autoescape:
498 result = Markup(result)
499 return result
500
501Context filters work the same just that the first argument is the current
Armin Ronacher19cf9c22008-05-01 12:49:53 +0200502active :class:`Context` rather then the environment.
Armin Ronacher7259c762008-04-30 13:03:59 +0200503
504
505.. _writing-tests:
506
507Custom Tests
508------------
509
510Tests work like filters just that there is no way for a filter to get access
511to the environment or context and that they can't be chained. The return
512value of a filter should be `True` or `False`. The purpose of a filter is to
513give the template designers the possibility to perform type and conformability
514checks.
515
516Here a simple filter that checks if a variable is a prime number::
517
518 import math
519
520 def is_prime(n):
521 if n == 2:
522 return True
523 for i in xrange(2, int(math.ceil(math.sqrt(n))) + 1):
524 if n % i == 0:
525 return False
526 return True
527
528
529You can register it on the template environment by updating the
530:attr:`~Environment.tests` dict on the environment::
531
532 environment.tests['prime'] = is_prime
533
534A template designer can then use the test like this:
535
536.. sourcecode:: jinja
537
538 {% if 42 is prime %}
539 42 is a prime number
540 {% else %}
541 42 is not a prime number
542 {% endif %}
543
544
545.. _global-namespace:
546
547The Global Namespace
548--------------------
549
Armin Ronacher981cbf62008-05-13 09:12:27 +0200550Variables stored in the :attr:`Environment.globals` dict are special as they
551are available for imported templates too, even if they are imported without
552context. This is the place where you can put variables and functions
553that should be available all the time. Additionally :attr:`Template.globals`
554exist that are variables available to a specific template that are available
555to all :meth:`~Template.render` calls.
Armin Ronacher5411ce72008-05-25 11:36:22 +0200556
557
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200558.. _low-level-api:
559
Armin Ronacher5411ce72008-05-25 11:36:22 +0200560Low Level API
561-------------
562
563The low level API exposes functionality that can be useful to understand some
564implementation details, debugging purposes or advanced :ref:`extension
Armin Ronacher61a5a242008-05-26 12:07:44 +0200565<jinja-extensions>` techniques. Unless you know exactly what you are doing we
566don't recommend using any of those.
Armin Ronacher5411ce72008-05-25 11:36:22 +0200567
568.. automethod:: Environment.lex
569
570.. automethod:: Environment.parse
571
572.. automethod:: Template.new_context
573
574.. method:: Template.root_render_func(context)
575
576 This is the low level render function. It's passed a :class:`Context`
577 that has to be created by :meth:`new_context` of the same template or
578 a compatible template. This render function is generated by the
579 compiler from the template code and returns a generator that yields
580 unicode strings.
581
582 If an exception in the template code happens the template engine will
583 not rewrite the exception but pass through the original one. As a
584 matter of fact this function should only be called from within a
585 :meth:`render` / :meth:`generate` / :meth:`stream` call.
586
587.. attribute:: Template.blocks
588
589 A dict of block render functions. Each of these functions works exactly
590 like the :meth:`root_render_func` with the same limitations.
591
592.. attribute:: Template.is_up_to_date
593
594 This attribute is `False` if there is a newer version of the template
595 available, otherwise `True`.
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200596
597.. admonition:: Note
598
599 The low-level API is fragile. Future Jinja2 versions will not change it
600 in a backwards incompatible way but modifications in the Jinja core may
601 shine through. For example if Jinja2 introduces a new AST node in later
602 versions that may be returned by :meth:`~Environment.parse`.