Edgewall Software

Changes between Version 28 and Version 29 of TracDev/PortingFromGenshiToJinja


Ignore:
Timestamp:
Jan 6, 2017, 5:03:25 PM (7 years ago)
Author:
Christian Boos
Comment:

Added #TheJinja2syntax section describing the customization we use

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/PortingFromGenshiToJinja

    v28 v29  
    44The following documentation corresponds to the [Proposals/Jinja Jinja] development proposal; it will be in Trac 1.3.x if all goes well \\
    55(Git branch: [log:cboos.git@jinja2] - [https://github.com/cboos/trac.git github mirror]).
     6
     7Track the integration progress in #12639.
    68}}}
    79
     
    1416Compare the 'j...' Jinja templates found in source:cboos.git/trac/templates@jinja2 with their corresponding Genshi ones.
    1517
    16 In the first part of this document, we try to cover all the Genshi features used by Trac and present their Jinja2 equivalent. Whenever possible, we tried to minimize these differences by customizing the Jinja2 syntax. For example, we use `${...}` for variable expansion, like Genshi does, instead of `{{...}}`. Another aspect of our usage convention is that we favor [#ifthenelse line statements] over `{% ... %}`. So even someone familiar with the "default" Jinja2 syntax should glance through this document to see how "we" use Jinja2.
     18In the first part of this document, we try to cover all the Genshi features used by Trac and present their Jinja2 equivalent. Whenever possible, we tried to minimize these differences by customizing the Jinja2 syntax. For example, we use `${...}` for variable expansion, like Genshi does, instead of `{{...}}`. Another aspect of our usage convention is that we favor [#ifthenelse line statements] over `{% ... %}`. So even someone familiar with the "default" Jinja2 syntax should glance through this document to see how "we" use Jinja2, as summarized in the table below.
    1719
    1820The last part of the document describes the Python code changes, focusing notably on how to replace the deprecated `ITemplateStreamFilter` interface.
     
    2022Note that Genshi will be supported concurrently with Jinja2 only for a short while, including the deprecated `ITemplateStreamFilter` interface, but probably only during the 1.3.x development period, and at most for the 1.4-stable period. If for some reason you're stuck to having to support Genshi templates, you'll have to stick to Trac 1.2.x or 1.3.x. But you really should make the transition effort as Jinja2 templates are 5-10x faster than their Genshi equivalent, for only a 1/5th of the cost in memory usage.
    2123
    22 
     24== The Jinja2 syntax
     25
     26The Jinja2 template engine is quite flexible and its syntax can be customized to some extent. We took this opportunity to make it as close as possible to the Genshi template syntax, in particular for the variable expansion.
     27
     28We use the following configuration:
     29|| key || value || example / explanation
     30|| extensions ||  jinja2.ext.with_  jinja2.ext.i18n  || `with` directive can be used ([#with more])
     31|| block_start_string ||  {%  || \
     32{{{#!td rowspan=2
     33`{% if cond %} value {% endif %}` possible (but discouraged)
     34}}}
     35|--
     36|| block_end_string ||  %}  ||
     37|| variable_start_string ||  ${  || \
     38{{{#!td rowspan=2
     39`${the_variable}` (but not `$the_variable`)
     40([#expandavariable more])
     41}}}
     42|--
     43|| variable_end_string ||  }  ||
     44|| line_statement_prefix ||  #  || \
     45{{{#!td
     46{{{
     47# if cond:
     48value
     49# endif
     50}}}
     51(preferred form) ([#if more])
     52}}}
     53|--
     54|| line_comment_prefix ||  ##  || `## comments are good`
     55|| trim_blocks ||  yes  || whitespace removal after a block
     56|| lstrip_blocks ||  yes  || whitespace removal before a block
     57|| newstyle_gettext ||  yes  || i.e. like the Trac `gettext`
    2358
    2459== Changes in the HTML
     
    95130
    96131It is also possible to pass "parameters" to included templates.
    97 Those are not actually declared parameters, simply variables expected to be available in the template that can be set for the scope of one specific include. This works exactly like in Genshi, with a with statement:
     132Those are not actually declared parameters, simply variables expected to be available in the template that can be set for the scope of one specific include. This works exactly like in Genshi, with a `with` statement:
    98133
    99134 - Genshi
     
    165200If you really have to, you can also use the block style:
    166201{{{#!html+jinja
    167 {% if flag %}<b>OK</b>{% else %}<i>!!!</i>{% endif %}
     202{{ if flag }}<b>OK</b>{{ else }}<i>!!!</i>{{ endif }}
    168203}}}
    169204However this goes against readability and processing via the [./Checker jinjachecker] tool,  so we really advise that you stick to the use of //[http://jinja.pocoo.org/docs/dev/templates/#line-statements line statements]//.