Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
Armin Ronacher | 82b3f3d | 2008-03-31 20:01:08 +0200 | [diff] [blame] | 3 | jinja2.environment |
| 4 | ~~~~~~~~~~~~~~~~~~ |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 5 | |
| 6 | Provides a class that holds runtime and parsing time options. |
| 7 | |
Armin Ronacher | 55494e4 | 2010-01-22 09:41:48 +0100 | [diff] [blame] | 8 | :copyright: (c) 2010 by the Jinja Team. |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 9 | :license: BSD, see LICENSE for more details. |
| 10 | """ |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 11 | import sys |
Armin Ronacher | ba6e25a | 2008-11-02 15:58:14 +0100 | [diff] [blame] | 12 | from jinja2 import nodes |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 13 | from jinja2.defaults import * |
Armin Ronacher | 9a0078d | 2008-08-13 18:24:17 +0200 | [diff] [blame] | 14 | from jinja2.lexer import get_lexer, TokenStream |
Armin Ronacher | 0553093 | 2008-04-20 13:27:49 +0200 | [diff] [blame] | 15 | from jinja2.parser import Parser |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 16 | from jinja2.optimizer import optimize |
| 17 | from jinja2.compiler import generate |
Armin Ronacher | 74a0cd9 | 2009-02-19 15:56:53 +0100 | [diff] [blame] | 18 | from jinja2.runtime import Undefined, new_context |
Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 19 | from jinja2.exceptions import TemplateSyntaxError, TemplateNotFound, \ |
| 20 | TemplatesNotFound |
Armin Ronacher | ba6e25a | 2008-11-02 15:58:14 +0100 | [diff] [blame] | 21 | from jinja2.utils import import_string, LRUCache, Markup, missing, \ |
Armin Ronacher | d416a97 | 2009-02-24 22:58:00 +0100 | [diff] [blame] | 22 | concat, consume, internalcode |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 23 | |
| 24 | |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 25 | # for direct template usage we have up to ten living environments |
| 26 | _spontaneous_environments = LRUCache(10) |
| 27 | |
Armin Ronacher | a18872d | 2009-03-05 23:47:00 +0100 | [diff] [blame] | 28 | # the function to create jinja traceback objects. This is dynamically |
| 29 | # imported on the first exception in the exception handler. |
| 30 | _make_traceback = None |
| 31 | |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 32 | |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 33 | def get_spontaneous_environment(*args): |
Georg Brandl | 3e497b7 | 2008-09-19 09:55:17 +0000 | [diff] [blame] | 34 | """Return a new spontaneous environment. A spontaneous environment is an |
| 35 | unnamed and unaccessible (in theory) environment that is used for |
| 36 | templates generated from a string and not from the file system. |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 37 | """ |
| 38 | try: |
| 39 | env = _spontaneous_environments.get(args) |
| 40 | except TypeError: |
| 41 | return Environment(*args) |
| 42 | if env is not None: |
| 43 | return env |
| 44 | _spontaneous_environments[args] = env = Environment(*args) |
Armin Ronacher | c9705c2 | 2008-04-27 21:28:03 +0200 | [diff] [blame] | 45 | env.shared = True |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 46 | return env |
| 47 | |
| 48 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 49 | def create_cache(size): |
| 50 | """Return the cache class for the given size.""" |
| 51 | if size == 0: |
| 52 | return None |
| 53 | if size < 0: |
| 54 | return {} |
| 55 | return LRUCache(size) |
| 56 | |
| 57 | |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 58 | def copy_cache(cache): |
| 59 | """Create an empty copy of the given cache.""" |
| 60 | if cache is None: |
Armin Ronacher | 2bc1ef7 | 2008-12-08 15:21:26 +0100 | [diff] [blame] | 61 | return None |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 62 | elif type(cache) is dict: |
| 63 | return {} |
| 64 | return LRUCache(cache.capacity) |
| 65 | |
| 66 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 67 | def load_extensions(environment, extensions): |
| 68 | """Load the extensions from the list and bind it to the environment. |
Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 69 | Returns a dict of instanciated environments. |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 70 | """ |
Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 71 | result = {} |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 72 | for extension in extensions: |
| 73 | if isinstance(extension, basestring): |
| 74 | extension = import_string(extension) |
Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 75 | result[extension.identifier] = extension(environment) |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 76 | return result |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 77 | |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 78 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 79 | def _environment_sanity_check(environment): |
| 80 | """Perform a sanity check on the environment.""" |
| 81 | assert issubclass(environment.undefined, Undefined), 'undefined must ' \ |
| 82 | 'be a subclass of undefined because filters depend on it.' |
| 83 | assert environment.block_start_string != \ |
| 84 | environment.variable_start_string != \ |
| 85 | environment.comment_start_string, 'block, variable and comment ' \ |
| 86 | 'start strings must be different' |
Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 87 | assert environment.newline_sequence in ('\r', '\r\n', '\n'), \ |
| 88 | 'newline_sequence set to unknown line ending string.' |
Armin Ronacher | 19cf9c2 | 2008-05-01 12:49:53 +0200 | [diff] [blame] | 89 | return environment |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 90 | |
| 91 | |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 92 | class Environment(object): |
Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 93 | r"""The core component of Jinja is the `Environment`. It contains |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 94 | important shared variables like configuration, filters, tests, |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 95 | globals and others. Instances of this class may be modified if |
| 96 | they are not shared and if no template was loaded so far. |
| 97 | Modifications on environments after the first template was loaded |
| 98 | will lead to surprising effects and undefined behavior. |
| 99 | |
| 100 | Here the possible initialization parameters: |
| 101 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 102 | `block_start_string` |
| 103 | The string marking the begin of a block. Defaults to ``'{%'``. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 104 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 105 | `block_end_string` |
| 106 | The string marking the end of a block. Defaults to ``'%}'``. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 107 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 108 | `variable_start_string` |
| 109 | The string marking the begin of a print statement. |
| 110 | Defaults to ``'{{'``. |
Armin Ronacher | 115de2e | 2008-05-01 22:20:05 +0200 | [diff] [blame] | 111 | |
Armin Ronacher | 63fd798 | 2008-06-20 18:47:56 +0200 | [diff] [blame] | 112 | `variable_end_string` |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 113 | The string marking the end of a print statement. Defaults to |
| 114 | ``'}}'``. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 115 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 116 | `comment_start_string` |
| 117 | The string marking the begin of a comment. Defaults to ``'{#'``. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 118 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 119 | `comment_end_string` |
| 120 | The string marking the end of a comment. Defaults to ``'#}'``. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 121 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 122 | `line_statement_prefix` |
| 123 | If given and a string, this will be used as prefix for line based |
| 124 | statements. See also :ref:`line-statements`. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 125 | |
Armin Ronacher | 59b6bd5 | 2009-03-30 21:00:16 +0200 | [diff] [blame] | 126 | `line_comment_prefix` |
| 127 | If given and a string, this will be used as prefix for line based |
| 128 | based comments. See also :ref:`line-statements`. |
| 129 | |
| 130 | .. versionadded:: 2.2 |
| 131 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 132 | `trim_blocks` |
| 133 | If this is set to ``True`` the first newline after a block is |
| 134 | removed (block, not variable tag!). Defaults to `False`. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 135 | |
Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 136 | `newline_sequence` |
| 137 | The sequence that starts a newline. Must be one of ``'\r'``, |
| 138 | ``'\n'`` or ``'\r\n'``. The default is ``'\n'`` which is a |
| 139 | useful default for Linux and OS X systems as well as web |
| 140 | applications. |
| 141 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 142 | `extensions` |
| 143 | List of Jinja extensions to use. This can either be import paths |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 144 | as strings or extension classes. For more information have a |
| 145 | look at :ref:`the extensions documentation <jinja-extensions>`. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 146 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 147 | `optimized` |
| 148 | should the optimizer be enabled? Default is `True`. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 149 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 150 | `undefined` |
| 151 | :class:`Undefined` or a subclass of it that is used to represent |
| 152 | undefined values in the template. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 153 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 154 | `finalize` |
Armin Ronacher | d9ea26e | 2010-01-24 14:29:26 +0100 | [diff] [blame] | 155 | A callable that can be used to process the result of a variable |
| 156 | expression before it is output. For example one can convert |
| 157 | `None` implicitly into an empty string here. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 158 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 159 | `autoescape` |
| 160 | If set to true the XML/HTML autoescaping feature is enabled. |
Armin Ronacher | f7e405d | 2008-09-08 23:57:26 +0200 | [diff] [blame] | 161 | For more details about auto escaping see |
| 162 | :class:`~jinja2.utils.Markup`. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 163 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 164 | `loader` |
| 165 | The template loader for this environment. |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 166 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 167 | `cache_size` |
| 168 | The size of the cache. Per default this is ``50`` which means |
| 169 | that if more than 50 templates are loaded the loader will clean |
| 170 | out the least recently used template. If the cache size is set to |
| 171 | ``0`` templates are recompiled all the time, if the cache size is |
| 172 | ``-1`` the cache will not be cleaned. |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 173 | |
Armin Ronacher | 7b5680c | 2008-05-06 16:54:22 +0200 | [diff] [blame] | 174 | `auto_reload` |
| 175 | Some loaders load templates from locations where the template |
| 176 | sources may change (ie: file system or database). If |
| 177 | `auto_reload` is set to `True` (default) every time a template is |
| 178 | requested the loader checks if the source changed and if yes, it |
| 179 | will reload the template. For higher performance it's possible to |
| 180 | disable that. |
Armin Ronacher | 4d5bdff | 2008-09-17 16:19:46 +0200 | [diff] [blame] | 181 | |
| 182 | `bytecode_cache` |
| 183 | If set to a bytecode cache object, this object will provide a |
| 184 | cache for the internal Jinja bytecode so that templates don't |
| 185 | have to be parsed if they were not changed. |
Armin Ronacher | a816bf4 | 2008-09-17 21:28:01 +0200 | [diff] [blame] | 186 | |
| 187 | See :ref:`bytecode-cache` for more information. |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 188 | """ |
| 189 | |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 190 | #: if this environment is sandboxed. Modifying this variable won't make |
| 191 | #: the environment sandboxed though. For a real sandboxed environment |
| 192 | #: have a look at jinja2.sandbox |
| 193 | sandboxed = False |
| 194 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 195 | #: True if the environment is just an overlay |
Armin Ronacher | 619eeed | 2009-07-09 21:55:29 +0200 | [diff] [blame] | 196 | overlayed = False |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 197 | |
Armin Ronacher | 19cf9c2 | 2008-05-01 12:49:53 +0200 | [diff] [blame] | 198 | #: the environment this environment is linked to if it is an overlay |
| 199 | linked_to = None |
| 200 | |
Armin Ronacher | c9705c2 | 2008-04-27 21:28:03 +0200 | [diff] [blame] | 201 | #: shared environments have this set to `True`. A shared environment |
| 202 | #: must not be modified |
| 203 | shared = False |
| 204 | |
Armin Ronacher | 32ed6c9 | 2009-04-02 14:04:41 +0200 | [diff] [blame] | 205 | #: these are currently EXPERIMENTAL undocumented features. |
Armin Ronacher | a18872d | 2009-03-05 23:47:00 +0100 | [diff] [blame] | 206 | exception_handler = None |
| 207 | exception_formatter = None |
| 208 | |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 209 | def __init__(self, |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 210 | block_start_string=BLOCK_START_STRING, |
| 211 | block_end_string=BLOCK_END_STRING, |
| 212 | variable_start_string=VARIABLE_START_STRING, |
| 213 | variable_end_string=VARIABLE_END_STRING, |
| 214 | comment_start_string=COMMENT_START_STRING, |
| 215 | comment_end_string=COMMENT_END_STRING, |
| 216 | line_statement_prefix=LINE_STATEMENT_PREFIX, |
Armin Ronacher | 59b6bd5 | 2009-03-30 21:00:16 +0200 | [diff] [blame] | 217 | line_comment_prefix=LINE_COMMENT_PREFIX, |
Armin Ronacher | 4f5008f | 2008-05-23 23:36:07 +0200 | [diff] [blame] | 218 | trim_blocks=TRIM_BLOCKS, |
| 219 | newline_sequence=NEWLINE_SEQUENCE, |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 220 | extensions=(), |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 221 | optimized=True, |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 222 | undefined=Undefined, |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 223 | finalize=None, |
| 224 | autoescape=False, |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 225 | loader=None, |
| 226 | cache_size=50, |
Armin Ronacher | 4d5bdff | 2008-09-17 16:19:46 +0200 | [diff] [blame] | 227 | auto_reload=True, |
| 228 | bytecode_cache=None): |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 229 | # !!Important notice!! |
| 230 | # The constructor accepts quite a few arguments that should be |
| 231 | # passed by keyword rather than position. However it's important to |
| 232 | # not change the order of arguments because it's used at least |
| 233 | # internally in those cases: |
| 234 | # - spontaneus environments (i18n extension and Template) |
| 235 | # - unittests |
| 236 | # If parameter changes are required only add parameters at the end |
| 237 | # and don't change the arguments (or the defaults!) of the arguments |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 238 | # existing already. |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 239 | |
| 240 | # lexer / parser information |
| 241 | self.block_start_string = block_start_string |
| 242 | self.block_end_string = block_end_string |
| 243 | self.variable_start_string = variable_start_string |
| 244 | self.variable_end_string = variable_end_string |
| 245 | self.comment_start_string = comment_start_string |
| 246 | self.comment_end_string = comment_end_string |
Armin Ronacher | bf7c4ad | 2008-04-12 12:02:36 +0200 | [diff] [blame] | 247 | self.line_statement_prefix = line_statement_prefix |
Armin Ronacher | 59b6bd5 | 2009-03-30 21:00:16 +0200 | [diff] [blame] | 248 | self.line_comment_prefix = line_comment_prefix |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 249 | self.trim_blocks = trim_blocks |
Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 250 | self.newline_sequence = newline_sequence |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 251 | |
Armin Ronacher | f59bac2 | 2008-04-20 13:11:43 +0200 | [diff] [blame] | 252 | # runtime information |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 253 | self.undefined = undefined |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 254 | self.optimized = optimized |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame] | 255 | self.finalize = finalize |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 256 | self.autoescape = autoescape |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 257 | |
| 258 | # defaults |
| 259 | self.filters = DEFAULT_FILTERS.copy() |
| 260 | self.tests = DEFAULT_TESTS.copy() |
| 261 | self.globals = DEFAULT_NAMESPACE.copy() |
| 262 | |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 263 | # set the loader provided |
| 264 | self.loader = loader |
Armin Ronacher | 4d5bdff | 2008-09-17 16:19:46 +0200 | [diff] [blame] | 265 | self.bytecode_cache = None |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 266 | self.cache = create_cache(cache_size) |
Armin Ronacher | 4d5bdff | 2008-09-17 16:19:46 +0200 | [diff] [blame] | 267 | self.bytecode_cache = bytecode_cache |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 268 | self.auto_reload = auto_reload |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 269 | |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 270 | # load extensions |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 271 | self.extensions = load_extensions(self, extensions) |
| 272 | |
| 273 | _environment_sanity_check(self) |
| 274 | |
Armin Ronacher | 762079c | 2008-05-08 23:57:56 +0200 | [diff] [blame] | 275 | def extend(self, **attributes): |
| 276 | """Add the items to the instance of the environment if they do not exist |
| 277 | yet. This is used by :ref:`extensions <writing-extensions>` to register |
| 278 | callbacks and configuration values without breaking inheritance. |
| 279 | """ |
| 280 | for key, value in attributes.iteritems(): |
| 281 | if not hasattr(self, key): |
| 282 | setattr(self, key, value) |
| 283 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 284 | def overlay(self, block_start_string=missing, block_end_string=missing, |
| 285 | variable_start_string=missing, variable_end_string=missing, |
| 286 | comment_start_string=missing, comment_end_string=missing, |
Armin Ronacher | 59b6bd5 | 2009-03-30 21:00:16 +0200 | [diff] [blame] | 287 | line_statement_prefix=missing, line_comment_prefix=missing, |
| 288 | trim_blocks=missing, extensions=missing, optimized=missing, |
| 289 | undefined=missing, finalize=missing, autoescape=missing, |
| 290 | loader=missing, cache_size=missing, auto_reload=missing, |
Armin Ronacher | 4d5bdff | 2008-09-17 16:19:46 +0200 | [diff] [blame] | 291 | bytecode_cache=missing): |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 292 | """Create a new overlay environment that shares all the data with the |
Georg Brandl | 95632c4 | 2009-11-22 18:35:18 +0100 | [diff] [blame] | 293 | current environment except of cache and the overridden attributes. |
| 294 | Extensions cannot be removed for an overlayed environment. An overlayed |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 295 | environment automatically gets all the extensions of the environment it |
| 296 | is linked to plus optional extra extensions. |
| 297 | |
| 298 | Creating overlays should happen after the initial environment was set |
| 299 | up completely. Not all attributes are truly linked, some are just |
| 300 | copied over so modifications on the original environment may not shine |
| 301 | through. |
| 302 | """ |
| 303 | args = dict(locals()) |
| 304 | del args['self'], args['cache_size'], args['extensions'] |
| 305 | |
| 306 | rv = object.__new__(self.__class__) |
| 307 | rv.__dict__.update(self.__dict__) |
Armin Ronacher | 619eeed | 2009-07-09 21:55:29 +0200 | [diff] [blame] | 308 | rv.overlayed = True |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 309 | rv.linked_to = self |
| 310 | |
| 311 | for key, value in args.iteritems(): |
| 312 | if value is not missing: |
| 313 | setattr(rv, key, value) |
| 314 | |
| 315 | if cache_size is not missing: |
| 316 | rv.cache = create_cache(cache_size) |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 317 | else: |
| 318 | rv.cache = copy_cache(self.cache) |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 319 | |
Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 320 | rv.extensions = {} |
| 321 | for key, value in self.extensions.iteritems(): |
| 322 | rv.extensions[key] = value.bind(rv) |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 323 | if extensions is not missing: |
Armin Ronacher | 023b5e9 | 2008-05-08 11:03:10 +0200 | [diff] [blame] | 324 | rv.extensions.update(load_extensions(extensions)) |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 325 | |
Armin Ronacher | 19cf9c2 | 2008-05-01 12:49:53 +0200 | [diff] [blame] | 326 | return _environment_sanity_check(rv) |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 327 | |
Armin Ronacher | 9a0078d | 2008-08-13 18:24:17 +0200 | [diff] [blame] | 328 | lexer = property(get_lexer, doc="The lexer for this environment.") |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 329 | |
Armin Ronacher | 6dc6f29 | 2008-06-12 08:50:07 +0200 | [diff] [blame] | 330 | def getitem(self, obj, argument): |
| 331 | """Get an item or attribute of an object but prefer the item.""" |
Armin Ronacher | 08a6a3b | 2008-05-13 15:35:47 +0200 | [diff] [blame] | 332 | try: |
| 333 | return obj[argument] |
| 334 | except (TypeError, LookupError): |
Armin Ronacher | f15f5f7 | 2008-05-26 12:21:45 +0200 | [diff] [blame] | 335 | if isinstance(argument, basestring): |
| 336 | try: |
| 337 | attr = str(argument) |
| 338 | except: |
| 339 | pass |
| 340 | else: |
| 341 | try: |
| 342 | return getattr(obj, attr) |
| 343 | except AttributeError: |
| 344 | pass |
Armin Ronacher | 08a6a3b | 2008-05-13 15:35:47 +0200 | [diff] [blame] | 345 | return self.undefined(obj=obj, name=argument) |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 346 | |
Armin Ronacher | 6dc6f29 | 2008-06-12 08:50:07 +0200 | [diff] [blame] | 347 | def getattr(self, obj, attribute): |
| 348 | """Get an item or attribute of an object but prefer the attribute. |
| 349 | Unlike :meth:`getitem` the attribute *must* be a bytestring. |
| 350 | """ |
| 351 | try: |
| 352 | return getattr(obj, attribute) |
| 353 | except AttributeError: |
| 354 | pass |
| 355 | try: |
| 356 | return obj[attribute] |
Christopher Grebs | f1c940f | 2008-07-10 11:52:17 +0200 | [diff] [blame] | 357 | except (TypeError, LookupError, AttributeError): |
Armin Ronacher | 6dc6f29 | 2008-06-12 08:50:07 +0200 | [diff] [blame] | 358 | return self.undefined(obj=obj, name=attribute) |
| 359 | |
Armin Ronacher | d416a97 | 2009-02-24 22:58:00 +0100 | [diff] [blame] | 360 | @internalcode |
Armin Ronacher | 7f15ef8 | 2008-05-16 09:11:39 +0200 | [diff] [blame] | 361 | def parse(self, source, name=None, filename=None): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 362 | """Parse the sourcecode and return the abstract syntax tree. This |
| 363 | tree of nodes is used by the compiler to convert the template into |
| 364 | executable source- or bytecode. This is useful for debugging or to |
| 365 | extract information from templates. |
Armin Ronacher | ed98cac | 2008-05-07 08:42:11 +0200 | [diff] [blame] | 366 | |
| 367 | If you are :ref:`developing Jinja2 extensions <writing-extensions>` |
| 368 | this gives you a good overview of the node tree generated. |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 369 | """ |
Armin Ronacher | aaf010d | 2008-05-01 13:14:30 +0200 | [diff] [blame] | 370 | try: |
Armin Ronacher | efcc0e5 | 2009-09-13 00:22:50 -0700 | [diff] [blame] | 371 | return self._parse(source, name, filename) |
Armin Ronacher | 2a79192 | 2009-04-16 23:15:22 +0200 | [diff] [blame] | 372 | except TemplateSyntaxError: |
Armin Ronacher | bd35772 | 2009-08-05 20:25:06 +0200 | [diff] [blame] | 373 | exc_info = sys.exc_info() |
| 374 | self.handle_exception(exc_info, source_hint=source) |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 375 | |
Armin Ronacher | efcc0e5 | 2009-09-13 00:22:50 -0700 | [diff] [blame] | 376 | def _parse(self, source, name, filename): |
| 377 | """Internal parsing function used by `parse` and `compile`.""" |
| 378 | if isinstance(filename, unicode): |
| 379 | filename = filename.encode('utf-8') |
| 380 | return Parser(self, source, name, filename).parse() |
| 381 | |
Armin Ronacher | 7f15ef8 | 2008-05-16 09:11:39 +0200 | [diff] [blame] | 382 | def lex(self, source, name=None, filename=None): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 383 | """Lex the given sourcecode and return a generator that yields |
| 384 | tokens as tuples in the form ``(lineno, token_type, value)``. |
Armin Ronacher | 5cdc1ac | 2008-05-07 12:17:18 +0200 | [diff] [blame] | 385 | This can be useful for :ref:`extension development <writing-extensions>` |
| 386 | and debugging templates. |
Armin Ronacher | 9ad96e7 | 2008-06-13 22:44:01 +0200 | [diff] [blame] | 387 | |
| 388 | This does not perform preprocessing. If you want the preprocessing |
| 389 | of the extensions to be applied you have to filter source through |
| 390 | the :meth:`preprocess` method. |
Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 391 | """ |
Armin Ronacher | ccae055 | 2008-10-05 23:08:58 +0200 | [diff] [blame] | 392 | source = unicode(source) |
| 393 | try: |
| 394 | return self.lexer.tokeniter(source, name, filename) |
Armin Ronacher | 2a79192 | 2009-04-16 23:15:22 +0200 | [diff] [blame] | 395 | except TemplateSyntaxError: |
Armin Ronacher | bd35772 | 2009-08-05 20:25:06 +0200 | [diff] [blame] | 396 | exc_info = sys.exc_info() |
| 397 | self.handle_exception(exc_info, source_hint=source) |
Armin Ronacher | 9ad96e7 | 2008-06-13 22:44:01 +0200 | [diff] [blame] | 398 | |
| 399 | def preprocess(self, source, name=None, filename=None): |
| 400 | """Preprocesses the source with all extensions. This is automatically |
| 401 | called for all parsing and compiling methods but *not* for :meth:`lex` |
| 402 | because there you usually only want the actual source tokenized. |
| 403 | """ |
| 404 | return reduce(lambda s, e: e.preprocess(s, name, filename), |
| 405 | self.extensions.itervalues(), unicode(source)) |
| 406 | |
Armin Ronacher | ba6e25a | 2008-11-02 15:58:14 +0100 | [diff] [blame] | 407 | def _tokenize(self, source, name, filename=None, state=None): |
Armin Ronacher | 9ad96e7 | 2008-06-13 22:44:01 +0200 | [diff] [blame] | 408 | """Called by the parser to do the preprocessing and filtering |
| 409 | for all the extensions. Returns a :class:`~jinja2.lexer.TokenStream`. |
| 410 | """ |
Armin Ronacher | 9ad96e7 | 2008-06-13 22:44:01 +0200 | [diff] [blame] | 411 | source = self.preprocess(source, name, filename) |
Armin Ronacher | ba6e25a | 2008-11-02 15:58:14 +0100 | [diff] [blame] | 412 | stream = self.lexer.tokenize(source, name, filename, state) |
Armin Ronacher | 9ad96e7 | 2008-06-13 22:44:01 +0200 | [diff] [blame] | 413 | for ext in self.extensions.itervalues(): |
Armin Ronacher | 3e3a9be | 2008-06-14 12:44:15 +0200 | [diff] [blame] | 414 | stream = ext.filter_stream(stream) |
| 415 | if not isinstance(stream, TokenStream): |
| 416 | stream = TokenStream(stream, name, filename) |
Armin Ronacher | 9ad96e7 | 2008-06-13 22:44:01 +0200 | [diff] [blame] | 417 | return stream |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 418 | |
Armin Ronacher | d416a97 | 2009-02-24 22:58:00 +0100 | [diff] [blame] | 419 | @internalcode |
Armin Ronacher | 981cbf6 | 2008-05-13 09:12:27 +0200 | [diff] [blame] | 420 | def compile(self, source, name=None, filename=None, raw=False): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 421 | """Compile a node or template source code. The `name` parameter is |
| 422 | the load name of the template after it was joined using |
| 423 | :meth:`join_path` if necessary, not the filename on the file system. |
| 424 | the `filename` parameter is the estimated filename of the template on |
| 425 | the file system. If the template came from a database or memory this |
Armin Ronacher | 981cbf6 | 2008-05-13 09:12:27 +0200 | [diff] [blame] | 426 | can be omitted. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 427 | |
| 428 | The return value of this method is a python code object. If the `raw` |
| 429 | parameter is `True` the return value will be a string with python |
| 430 | code equivalent to the bytecode returned otherwise. This method is |
| 431 | mainly used internally. |
Armin Ronacher | 68f7767 | 2008-04-17 11:50:39 +0200 | [diff] [blame] | 432 | """ |
Armin Ronacher | efcc0e5 | 2009-09-13 00:22:50 -0700 | [diff] [blame] | 433 | source_hint = None |
| 434 | try: |
| 435 | if isinstance(source, basestring): |
| 436 | source_hint = source |
| 437 | source = self._parse(source, name, filename) |
| 438 | if self.optimized: |
| 439 | source = optimize(source, self) |
| 440 | source = generate(source, self, name, filename) |
| 441 | if raw: |
| 442 | return source |
| 443 | if filename is None: |
| 444 | filename = '<template>' |
| 445 | elif isinstance(filename, unicode): |
| 446 | filename = filename.encode('utf-8') |
| 447 | return compile(source, filename, 'exec') |
| 448 | except TemplateSyntaxError: |
| 449 | exc_info = sys.exc_info() |
| 450 | self.handle_exception(exc_info, source_hint=source) |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 451 | |
Armin Ronacher | ba6e25a | 2008-11-02 15:58:14 +0100 | [diff] [blame] | 452 | def compile_expression(self, source, undefined_to_none=True): |
| 453 | """A handy helper method that returns a callable that accepts keyword |
| 454 | arguments that appear as variables in the expression. If called it |
| 455 | returns the result of the expression. |
| 456 | |
| 457 | This is useful if applications want to use the same rules as Jinja |
| 458 | in template "configuration files" or similar situations. |
| 459 | |
| 460 | Example usage: |
| 461 | |
| 462 | >>> env = Environment() |
| 463 | >>> expr = env.compile_expression('foo == 42') |
| 464 | >>> expr(foo=23) |
| 465 | False |
| 466 | >>> expr(foo=42) |
| 467 | True |
| 468 | |
| 469 | Per default the return value is converted to `None` if the |
| 470 | expression returns an undefined value. This can be changed |
| 471 | by setting `undefined_to_none` to `False`. |
| 472 | |
| 473 | >>> env.compile_expression('var')() is None |
| 474 | True |
| 475 | >>> env.compile_expression('var', undefined_to_none=False)() |
| 476 | Undefined |
| 477 | |
| 478 | **new in Jinja 2.1** |
| 479 | """ |
| 480 | parser = Parser(self, source, state='variable') |
Armin Ronacher | bd35772 | 2009-08-05 20:25:06 +0200 | [diff] [blame] | 481 | exc_info = None |
Armin Ronacher | ba6e25a | 2008-11-02 15:58:14 +0100 | [diff] [blame] | 482 | try: |
| 483 | expr = parser.parse_expression() |
| 484 | if not parser.stream.eos: |
| 485 | raise TemplateSyntaxError('chunk after expression', |
| 486 | parser.stream.current.lineno, |
| 487 | None, None) |
Armin Ronacher | 2a79192 | 2009-04-16 23:15:22 +0200 | [diff] [blame] | 488 | except TemplateSyntaxError: |
Armin Ronacher | bd35772 | 2009-08-05 20:25:06 +0200 | [diff] [blame] | 489 | exc_info = sys.exc_info() |
| 490 | if exc_info is not None: |
| 491 | self.handle_exception(exc_info, source_hint=source) |
Armin Ronacher | ba6e25a | 2008-11-02 15:58:14 +0100 | [diff] [blame] | 492 | body = [nodes.Assign(nodes.Name('result', 'store'), expr, lineno=1)] |
| 493 | template = self.from_string(nodes.Template(body, lineno=1)) |
| 494 | return TemplateExpression(template, undefined_to_none) |
| 495 | |
Armin Ronacher | a18872d | 2009-03-05 23:47:00 +0100 | [diff] [blame] | 496 | def handle_exception(self, exc_info=None, rendered=False, source_hint=None): |
| 497 | """Exception handling helper. This is used internally to either raise |
| 498 | rewritten exceptions or return a rendered traceback for the template. |
| 499 | """ |
| 500 | global _make_traceback |
| 501 | if exc_info is None: |
| 502 | exc_info = sys.exc_info() |
Armin Ronacher | 32ed6c9 | 2009-04-02 14:04:41 +0200 | [diff] [blame] | 503 | |
| 504 | # the debugging module is imported when it's used for the first time. |
| 505 | # we're doing a lot of stuff there and for applications that do not |
| 506 | # get any exceptions in template rendering there is no need to load |
| 507 | # all of that. |
Armin Ronacher | a18872d | 2009-03-05 23:47:00 +0100 | [diff] [blame] | 508 | if _make_traceback is None: |
| 509 | from jinja2.debug import make_traceback as _make_traceback |
| 510 | traceback = _make_traceback(exc_info, source_hint) |
| 511 | if rendered and self.exception_formatter is not None: |
| 512 | return self.exception_formatter(traceback) |
| 513 | if self.exception_handler is not None: |
| 514 | self.exception_handler(traceback) |
| 515 | exc_type, exc_value, tb = traceback.standard_exc_info |
| 516 | raise exc_type, exc_value, tb |
| 517 | |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 518 | def join_path(self, template, parent): |
| 519 | """Join a template with the parent. By default all the lookups are |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 520 | relative to the loader root so this method returns the `template` |
| 521 | parameter unchanged, but if the paths should be relative to the |
| 522 | parent template, this function can be used to calculate the real |
| 523 | template name. |
| 524 | |
| 525 | Subclasses may override this method and implement template path |
| 526 | joining here. |
| 527 | """ |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 528 | return template |
| 529 | |
Armin Ronacher | d416a97 | 2009-02-24 22:58:00 +0100 | [diff] [blame] | 530 | @internalcode |
Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 531 | def _load_template(self, name, globals): |
| 532 | if self.loader is None: |
| 533 | raise TypeError('no loader for this environment specified') |
| 534 | if self.cache is not None: |
| 535 | template = self.cache.get(name) |
| 536 | if template is not None and (not self.auto_reload or \ |
| 537 | template.is_up_to_date): |
| 538 | return template |
| 539 | template = self.loader.load(self, name, globals) |
| 540 | if self.cache is not None: |
| 541 | self.cache[name] = template |
| 542 | return template |
| 543 | |
| 544 | @internalcode |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 545 | def get_template(self, name, parent=None, globals=None): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 546 | """Load a template from the loader. If a loader is configured this |
| 547 | method ask the loader for the template and returns a :class:`Template`. |
| 548 | If the `parent` parameter is not `None`, :meth:`join_path` is called |
| 549 | to get the real template name before loading. |
| 550 | |
Armin Ronacher | 7a519ee | 2008-09-08 23:10:47 +0200 | [diff] [blame] | 551 | The `globals` parameter can be used to provide template wide globals. |
Armin Ronacher | 981cbf6 | 2008-05-13 09:12:27 +0200 | [diff] [blame] | 552 | These variables are available in the context at render time. |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 553 | |
| 554 | If the template does not exist a :exc:`TemplateNotFound` exception is |
| 555 | raised. |
| 556 | """ |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 557 | if parent is not None: |
| 558 | name = self.join_path(name, parent) |
Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 559 | return self._load_template(name, self.make_globals(globals)) |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 560 | |
Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 561 | @internalcode |
| 562 | def select_template(self, names, parent=None, globals=None): |
| 563 | """Works like :meth:`get_template` but tries a number of templates |
| 564 | before it fails. If it cannot find any of the templates, it will |
| 565 | raise a :exc:`TemplatesNotFound` exception. |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 566 | |
Armin Ronacher | 31bbd9e | 2010-01-14 00:41:30 +0100 | [diff] [blame] | 567 | .. versionadded:: 2.2 |
| 568 | """ |
| 569 | if not names: |
| 570 | raise TemplatesNotFound(message=u'Tried to select from an empty list ' |
| 571 | u'of templates.') |
| 572 | globals = self.make_globals(globals) |
| 573 | for name in names: |
| 574 | if parent is not None: |
| 575 | name = self.join_path(name, parent) |
| 576 | try: |
| 577 | return self._load_template(name, globals) |
| 578 | except TemplateNotFound: |
| 579 | pass |
| 580 | raise TemplatesNotFound(names) |
| 581 | |
| 582 | @internalcode |
| 583 | def get_or_select_template(self, template_name_or_list, |
| 584 | parent=None, globals=None): |
| 585 | """ |
| 586 | Does a typecheck and dispatches to :meth:`select_template` if an |
| 587 | iterable of template names is given, otherwise to :meth:`get_template`. |
| 588 | |
| 589 | .. versionadded:: 2.2 |
| 590 | """ |
| 591 | if isinstance(template_name_or_list, basestring): |
| 592 | return self.get_template(template_name_or_list, parent, globals) |
| 593 | return self.select_template(template_name_or_list, parent, globals) |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 594 | |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 595 | def from_string(self, source, globals=None, template_class=None): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 596 | """Load a template from a string. This parses the source given and |
| 597 | returns a :class:`Template` object. |
| 598 | """ |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 599 | globals = self.make_globals(globals) |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 600 | cls = template_class or self.template_class |
Armin Ronacher | 981cbf6 | 2008-05-13 09:12:27 +0200 | [diff] [blame] | 601 | return cls.from_code(self, self.compile(source), globals, None) |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 602 | |
| 603 | def make_globals(self, d): |
| 604 | """Return a dict for the globals.""" |
Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 605 | if not d: |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 606 | return self.globals |
| 607 | return dict(self.globals, **d) |
Armin Ronacher | 46f5f98 | 2008-04-11 16:40:09 +0200 | [diff] [blame] | 608 | |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 609 | |
| 610 | class Template(object): |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 611 | """The central template object. This class represents a compiled template |
| 612 | and is used to evaluate it. |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 613 | |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 614 | Normally the template object is generated from an :class:`Environment` but |
| 615 | it also has a constructor that makes it possible to create a template |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 616 | instance directly using the constructor. It takes the same arguments as |
| 617 | the environment constructor but it's not possible to specify a loader. |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 618 | |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 619 | Every template object has a few methods and members that are guaranteed |
| 620 | to exist. However it's important that a template object should be |
| 621 | considered immutable. Modifications on the object are not supported. |
| 622 | |
| 623 | Template objects created from the constructor rather than an environment |
| 624 | do have an `environment` attribute that points to a temporary environment |
| 625 | that is probably shared with other templates created with the constructor |
| 626 | and compatible settings. |
| 627 | |
| 628 | >>> template = Template('Hello {{ name }}!') |
| 629 | >>> template.render(name='John Doe') |
| 630 | u'Hello John Doe!' |
| 631 | |
| 632 | >>> stream = template.stream(name='John Doe') |
| 633 | >>> stream.next() |
| 634 | u'Hello John Doe!' |
| 635 | >>> stream.next() |
| 636 | Traceback (most recent call last): |
| 637 | ... |
| 638 | StopIteration |
| 639 | """ |
| 640 | |
| 641 | def __new__(cls, source, |
Armin Ronacher | 4f5008f | 2008-05-23 23:36:07 +0200 | [diff] [blame] | 642 | block_start_string=BLOCK_START_STRING, |
| 643 | block_end_string=BLOCK_END_STRING, |
| 644 | variable_start_string=VARIABLE_START_STRING, |
| 645 | variable_end_string=VARIABLE_END_STRING, |
| 646 | comment_start_string=COMMENT_START_STRING, |
| 647 | comment_end_string=COMMENT_END_STRING, |
| 648 | line_statement_prefix=LINE_STATEMENT_PREFIX, |
Armin Ronacher | 59b6bd5 | 2009-03-30 21:00:16 +0200 | [diff] [blame] | 649 | line_comment_prefix=LINE_COMMENT_PREFIX, |
Armin Ronacher | 4f5008f | 2008-05-23 23:36:07 +0200 | [diff] [blame] | 650 | trim_blocks=TRIM_BLOCKS, |
| 651 | newline_sequence=NEWLINE_SEQUENCE, |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 652 | extensions=(), |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 653 | optimized=True, |
| 654 | undefined=Undefined, |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 655 | finalize=None, |
| 656 | autoescape=False): |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 657 | env = get_spontaneous_environment( |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 658 | block_start_string, block_end_string, variable_start_string, |
| 659 | variable_end_string, comment_start_string, comment_end_string, |
Armin Ronacher | 59b6bd5 | 2009-03-30 21:00:16 +0200 | [diff] [blame] | 660 | line_statement_prefix, line_comment_prefix, trim_blocks, |
| 661 | newline_sequence, frozenset(extensions), optimized, undefined, |
| 662 | finalize, autoescape, None, 0, False, None) |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 663 | return env.from_string(source, template_class=cls) |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 664 | |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 665 | @classmethod |
| 666 | def from_code(cls, environment, code, globals, uptodate=None): |
| 667 | """Creates a template object from compiled code and the globals. This |
| 668 | is used by the loaders and environment to create a template object. |
| 669 | """ |
| 670 | t = object.__new__(cls) |
| 671 | namespace = { |
| 672 | 'environment': environment, |
| 673 | '__jinja_template__': t |
| 674 | } |
| 675 | exec code in namespace |
| 676 | t.environment = environment |
Armin Ronacher | 771c750 | 2008-05-18 23:14:14 +0200 | [diff] [blame] | 677 | t.globals = globals |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 678 | t.name = namespace['name'] |
| 679 | t.filename = code.co_filename |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 680 | t.blocks = namespace['blocks'] |
Armin Ronacher | 771c750 | 2008-05-18 23:14:14 +0200 | [diff] [blame] | 681 | |
Georg Brandl | 3e497b7 | 2008-09-19 09:55:17 +0000 | [diff] [blame] | 682 | # render function and module |
Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 683 | t.root_render_func = namespace['root'] |
Armin Ronacher | 771c750 | 2008-05-18 23:14:14 +0200 | [diff] [blame] | 684 | t._module = None |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame] | 685 | |
| 686 | # debug and loader helpers |
| 687 | t._debug_info = namespace['debug_info'] |
| 688 | t._uptodate = uptodate |
| 689 | |
| 690 | return t |
| 691 | |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 692 | def render(self, *args, **kwargs): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 693 | """This method accepts the same arguments as the `dict` constructor: |
| 694 | A dict, a dict subclass or some keyword arguments. If no arguments |
| 695 | are given the context will be empty. These two calls do the same:: |
| 696 | |
| 697 | template.render(knights='that say nih') |
| 698 | template.render({'knights': 'that say nih'}) |
| 699 | |
| 700 | This will return the rendered template as unicode string. |
| 701 | """ |
Armin Ronacher | 771c750 | 2008-05-18 23:14:14 +0200 | [diff] [blame] | 702 | vars = dict(*args, **kwargs) |
Armin Ronacher | f41d139 | 2008-04-18 16:41:52 +0200 | [diff] [blame] | 703 | try: |
Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 704 | return concat(self.root_render_func(self.new_context(vars))) |
Armin Ronacher | f41d139 | 2008-04-18 16:41:52 +0200 | [diff] [blame] | 705 | except: |
Armin Ronacher | bd35772 | 2009-08-05 20:25:06 +0200 | [diff] [blame] | 706 | exc_info = sys.exc_info() |
| 707 | return self.environment.handle_exception(exc_info, True) |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 708 | |
| 709 | def stream(self, *args, **kwargs): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 710 | """Works exactly like :meth:`generate` but returns a |
| 711 | :class:`TemplateStream`. |
| 712 | """ |
Armin Ronacher | 19cf9c2 | 2008-05-01 12:49:53 +0200 | [diff] [blame] | 713 | return TemplateStream(self.generate(*args, **kwargs)) |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 714 | |
| 715 | def generate(self, *args, **kwargs): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 716 | """For very large templates it can be useful to not render the whole |
| 717 | template at once but evaluate each statement after another and yield |
| 718 | piece for piece. This method basically does exactly that and returns |
| 719 | a generator that yields one item after another as unicode strings. |
| 720 | |
| 721 | It accepts the same arguments as :meth:`render`. |
| 722 | """ |
Armin Ronacher | 771c750 | 2008-05-18 23:14:14 +0200 | [diff] [blame] | 723 | vars = dict(*args, **kwargs) |
Armin Ronacher | 19cf9c2 | 2008-05-01 12:49:53 +0200 | [diff] [blame] | 724 | try: |
Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 725 | for event in self.root_render_func(self.new_context(vars)): |
Armin Ronacher | 771c750 | 2008-05-18 23:14:14 +0200 | [diff] [blame] | 726 | yield event |
Armin Ronacher | 19cf9c2 | 2008-05-01 12:49:53 +0200 | [diff] [blame] | 727 | except: |
Armin Ronacher | bd35772 | 2009-08-05 20:25:06 +0200 | [diff] [blame] | 728 | exc_info = sys.exc_info() |
| 729 | else: |
| 730 | return |
| 731 | yield self.environment.handle_exception(exc_info, True) |
Armin Ronacher | 19cf9c2 | 2008-05-01 12:49:53 +0200 | [diff] [blame] | 732 | |
Armin Ronacher | 673aa88 | 2008-10-04 18:06:57 +0200 | [diff] [blame] | 733 | def new_context(self, vars=None, shared=False, locals=None): |
Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 734 | """Create a new :class:`Context` for this template. The vars |
Armin Ronacher | c9705c2 | 2008-04-27 21:28:03 +0200 | [diff] [blame] | 735 | provided will be passed to the template. Per default the globals |
Armin Ronacher | 673aa88 | 2008-10-04 18:06:57 +0200 | [diff] [blame] | 736 | are added to the context. If shared is set to `True` the data |
| 737 | is passed as it to the context without adding the globals. |
| 738 | |
| 739 | `locals` can be a dict of local variables for internal usage. |
Armin Ronacher | c9705c2 | 2008-04-27 21:28:03 +0200 | [diff] [blame] | 740 | """ |
Armin Ronacher | 74a0cd9 | 2009-02-19 15:56:53 +0100 | [diff] [blame] | 741 | return new_context(self.environment, self.name, self.blocks, |
| 742 | vars, shared, self.globals, locals) |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 743 | |
Armin Ronacher | 673aa88 | 2008-10-04 18:06:57 +0200 | [diff] [blame] | 744 | def make_module(self, vars=None, shared=False, locals=None): |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 745 | """This method works like the :attr:`module` attribute when called |
Armin Ronacher | 0aa0f58 | 2009-03-18 01:01:36 +0100 | [diff] [blame] | 746 | without arguments but it will evaluate the template on every call |
| 747 | rather than caching it. It's also possible to provide |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 748 | a dict which is then used as context. The arguments are the same |
Armin Ronacher | f3c35c4 | 2008-05-23 23:18:14 +0200 | [diff] [blame] | 749 | as for the :meth:`new_context` method. |
Armin Ronacher | ea847c5 | 2008-05-02 20:04:32 +0200 | [diff] [blame] | 750 | """ |
Armin Ronacher | 673aa88 | 2008-10-04 18:06:57 +0200 | [diff] [blame] | 751 | return TemplateModule(self, self.new_context(vars, shared, locals)) |
Armin Ronacher | ea847c5 | 2008-05-02 20:04:32 +0200 | [diff] [blame] | 752 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 753 | @property |
| 754 | def module(self): |
| 755 | """The template as module. This is used for imports in the |
| 756 | template runtime but is also useful if one wants to access |
| 757 | exported template variables from the Python layer: |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 758 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 759 | >>> t = Template('{% macro foo() %}42{% endmacro %}23') |
| 760 | >>> unicode(t.module) |
| 761 | u'23' |
| 762 | >>> t.module.foo() |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 763 | u'42' |
Armin Ronacher | 6ce170c | 2008-04-25 12:32:36 +0200 | [diff] [blame] | 764 | """ |
Armin Ronacher | 771c750 | 2008-05-18 23:14:14 +0200 | [diff] [blame] | 765 | if self._module is not None: |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 766 | return self._module |
Armin Ronacher | ea847c5 | 2008-05-02 20:04:32 +0200 | [diff] [blame] | 767 | self._module = rv = self.make_module() |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 768 | return rv |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 769 | |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 770 | def get_corresponding_lineno(self, lineno): |
| 771 | """Return the source line number of a line number in the |
| 772 | generated bytecode as they are not in sync. |
| 773 | """ |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 774 | for template_line, code_line in reversed(self.debug_info): |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 775 | if code_line <= lineno: |
| 776 | return template_line |
| 777 | return 1 |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 778 | |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 779 | @property |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 780 | def is_up_to_date(self): |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 781 | """If this variable is `False` there is a newer version available.""" |
Armin Ronacher | 814f6c2 | 2008-04-17 15:52:23 +0200 | [diff] [blame] | 782 | if self._uptodate is None: |
| 783 | return True |
| 784 | return self._uptodate() |
| 785 | |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 786 | @property |
| 787 | def debug_info(self): |
| 788 | """The debug info mapping.""" |
| 789 | return [tuple(map(int, x.split('='))) for x in |
| 790 | self._debug_info.split('&')] |
| 791 | |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 792 | def __repr__(self): |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 793 | if self.name is None: |
| 794 | name = 'memory:%x' % id(self) |
| 795 | else: |
| 796 | name = repr(self.name) |
| 797 | return '<%s %s>' % (self.__class__.__name__, name) |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 798 | |
| 799 | |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 800 | class TemplateModule(object): |
| 801 | """Represents an imported template. All the exported names of the |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 802 | template are available as attributes on this object. Additionally |
| 803 | converting it into an unicode- or bytestrings renders the contents. |
| 804 | """ |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 805 | |
| 806 | def __init__(self, template, context): |
Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 807 | self._body_stream = list(template.root_render_func(context)) |
Armin Ronacher | 6ce170c | 2008-04-25 12:32:36 +0200 | [diff] [blame] | 808 | self.__dict__.update(context.get_exported()) |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 809 | self.__name__ = template.name |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 810 | |
Armin Ronacher | bbbe062 | 2008-05-19 00:23:37 +0200 | [diff] [blame] | 811 | __unicode__ = lambda x: concat(x._body_stream) |
Armin Ronacher | 5411ce7 | 2008-05-25 11:36:22 +0200 | [diff] [blame] | 812 | __html__ = lambda x: Markup(concat(x._body_stream)) |
Armin Ronacher | 6ce170c | 2008-04-25 12:32:36 +0200 | [diff] [blame] | 813 | |
| 814 | def __str__(self): |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 815 | return unicode(self).encode('utf-8') |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 816 | |
| 817 | def __repr__(self): |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 818 | if self.__name__ is None: |
| 819 | name = 'memory:%x' % id(self) |
| 820 | else: |
Armin Ronacher | dc02b64 | 2008-05-15 22:47:27 +0200 | [diff] [blame] | 821 | name = repr(self.__name__) |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 822 | return '<%s %s>' % (self.__class__.__name__, name) |
Armin Ronacher | 963f97d | 2008-04-25 11:44:59 +0200 | [diff] [blame] | 823 | |
| 824 | |
Armin Ronacher | ba6e25a | 2008-11-02 15:58:14 +0100 | [diff] [blame] | 825 | class TemplateExpression(object): |
| 826 | """The :meth:`jinja2.Environment.compile_expression` method returns an |
| 827 | instance of this object. It encapsulates the expression-like access |
| 828 | to the template with an expression it wraps. |
| 829 | """ |
| 830 | |
| 831 | def __init__(self, template, undefined_to_none): |
| 832 | self._template = template |
| 833 | self._undefined_to_none = undefined_to_none |
| 834 | |
| 835 | def __call__(self, *args, **kwargs): |
| 836 | context = self._template.new_context(dict(*args, **kwargs)) |
| 837 | consume(self._template.root_render_func(context)) |
| 838 | rv = context.vars['result'] |
| 839 | if self._undefined_to_none and isinstance(rv, Undefined): |
| 840 | rv = None |
| 841 | return rv |
| 842 | |
| 843 | |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 844 | class TemplateStream(object): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 845 | """A template stream works pretty much like an ordinary python generator |
| 846 | but it can buffer multiple items to reduce the number of total iterations. |
| 847 | Per default the output is unbuffered which means that for every unbuffered |
| 848 | instruction in the template one unicode string is yielded. |
| 849 | |
| 850 | If buffering is enabled with a buffer size of 5, five items are combined |
| 851 | into a new unicode string. This is mainly useful if you are streaming |
| 852 | big templates to a client via WSGI which flushes after each iteration. |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 853 | """ |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 854 | |
| 855 | def __init__(self, gen): |
| 856 | self._gen = gen |
Armin Ronacher | 9cf9591 | 2008-05-24 19:54:43 +0200 | [diff] [blame] | 857 | self.disable_buffering() |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 858 | |
Armin Ronacher | 74b5106 | 2008-06-17 11:28:59 +0200 | [diff] [blame] | 859 | def dump(self, fp, encoding=None, errors='strict'): |
| 860 | """Dump the complete stream into a file or file-like object. |
| 861 | Per default unicode strings are written, if you want to encode |
| 862 | before writing specifiy an `encoding`. |
| 863 | |
| 864 | Example usage:: |
| 865 | |
| 866 | Template('Hello {{ name }}!').stream(name='foo').dump('hello.html') |
| 867 | """ |
| 868 | close = False |
| 869 | if isinstance(fp, basestring): |
| 870 | fp = file(fp, 'w') |
| 871 | close = True |
| 872 | try: |
| 873 | if encoding is not None: |
| 874 | iterable = (x.encode(encoding, errors) for x in self) |
| 875 | else: |
| 876 | iterable = self |
| 877 | if hasattr(fp, 'writelines'): |
| 878 | fp.writelines(iterable) |
| 879 | else: |
| 880 | for item in iterable: |
| 881 | fp.write(item) |
| 882 | finally: |
| 883 | if close: |
| 884 | fp.close() |
| 885 | |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 886 | def disable_buffering(self): |
| 887 | """Disable the output buffering.""" |
| 888 | self._next = self._gen.next |
| 889 | self.buffered = False |
| 890 | |
| 891 | def enable_buffering(self, size=5): |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 892 | """Enable buffering. Buffer `size` items before yielding them.""" |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 893 | if size <= 1: |
| 894 | raise ValueError('buffer size too small') |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 895 | |
Armin Ronacher | 5dfbfc1 | 2008-05-25 18:10:12 +0200 | [diff] [blame] | 896 | def generator(next): |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 897 | buf = [] |
| 898 | c_size = 0 |
| 899 | push = buf.append |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 900 | |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 901 | while 1: |
| 902 | try: |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 903 | while c_size < size: |
Armin Ronacher | 981cbf6 | 2008-05-13 09:12:27 +0200 | [diff] [blame] | 904 | c = next() |
| 905 | push(c) |
| 906 | if c: |
| 907 | c_size += 1 |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 908 | except StopIteration: |
| 909 | if not c_size: |
Armin Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 910 | return |
Armin Ronacher | de6bf71 | 2008-04-26 01:44:14 +0200 | [diff] [blame] | 911 | yield concat(buf) |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 912 | del buf[:] |
| 913 | c_size = 0 |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 914 | |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 915 | self.buffered = True |
Armin Ronacher | 5dfbfc1 | 2008-05-25 18:10:12 +0200 | [diff] [blame] | 916 | self._next = generator(self._gen.next).next |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 917 | |
| 918 | def __iter__(self): |
| 919 | return self |
| 920 | |
| 921 | def next(self): |
| 922 | return self._next() |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 923 | |
| 924 | |
| 925 | # hook in default template class. if anyone reads this comment: ignore that |
| 926 | # it's possible to use custom templates ;-) |
| 927 | Environment.template_class = Template |