| 246 | |
| 247 | === set several variables in a scope #with |
| 248 | |
| 249 | - Genshi |
| 250 | {{{#!html+genshi |
| 251 | <html py:with="is_query = report.sql.startswith('query:'); |
| 252 | new_report = action == 'new' and not is_query; |
| 253 | new_query = action == 'new' and is_query"> |
| 254 | ... |
| 255 | </html> |
| 256 | }}} |
| 257 | The variables are set for the scope of the element in which they are set (here <html>, |
| 258 | so the whole document) |
| 259 | - Jinja2 |
| 260 | {{{#!html+jinja |
| 261 | # with |
| 262 | # set is_query = report.sql.startswith('query:') |
| 263 | # set new_report = action == 'new' and not is_query |
| 264 | ... |
| 265 | # endwith |
| 266 | }}} |
| 267 | |
| 268 | But actually you will only use `with` in specific situations, like for wrapping an include directive (see [#include]). If you're already within a `for`, a `block` or a `macro`, the scope of the assignment is already limited to that of this directive. |
| 269 | |
| 270 | See [http://jinja.pocoo.org/docs/dev/templates/#assignments set] and [http://jinja.pocoo.org/docs/dev/templates/#with-statement with] docs. |
| 271 | |
| 272 | In addition, be careful when using `with`: don't wrap a `block` directive within a `with`. If you want to set a global scope for the document (like in our <html> example above), it's tempting to use a single `with` statement, for clarity. But that would wrap all blocks defined in the template and strange results would ensue. |
| 273 | |