Edgewall Software

Changes between Version 23 and Version 24 of TracDev/PortingFromGenshiToJinja


Ignore:
Timestamp:
Mar 26, 2016, 8:42:19 PM (8 years ago)
Author:
Christian Boos
Comment:

#with: add description and caveat

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/PortingFromGenshiToJinja

    v23 v24  
    244244   Note that we avoid using `>` in Jinja2 expressions as well, but simply to avoid that XML/HTML text editors get confused. We added a few custom tests for that (`greaterthan`, `greaterthanorequal`, `lessthan`, `lessthanorequal`).
    245245See [http://jinja.pocoo.org/docs/dev/templates/#tests tests] doc.
     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
     268But 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
     270See [http://jinja.pocoo.org/docs/dev/templates/#assignments set] and [http://jinja.pocoo.org/docs/dev/templates/#with-statement with] docs.
     271
     272In 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
    246274
    247275=== set HTML attributes   #htmlattr