Edgewall Software

Changes between Version 21 and Version 22 of TracDev/Proposals/Jinja


Ignore:
Timestamp:
Mar 18, 2016, 12:27:35 AM (8 years ago)
Author:
Christian Boos
Comment:

moved the #jinjachecker section to its own page

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/Proposals/Jinja

    v21 v22  
    1212
    1313[=#Status Status] of the branch (2016-03-17):
    14  - ported **63%** of the Genshi templates, 2 unit-tests failure (with mysql and psql), 8 functional tests failures
     14 - ported **63%** of the Genshi templates, 2 unit-tests failures (with mysql and psql), 8 functional tests failures
    1515 - clarify upgrade path for plugins that came to rely on `ITemplateStreamFilter`s?
    1616   (127/898 plugins (14.1%) on trac-hacks.org use `filter_stream()`)
     
    7878See also PortingFromGenshiToJinja/Example for a full example presented side-by-side.
    7979
    80 === `jinjachecker`
    81 
    82 Jinja2 is very helpful when it detects any kind of error, as you always end up with a meaningful backtrace. Nevertheless, it can be tedious to get the nesting of control structures right.
    83 
    84 To help with that, there's the [source:cboos.git/contrib/jinjachecker.py contrib/jinjachecker.py] tool.
    85 
    86 {{{
    87 python contrib/jinjachecker.py trac/wiki/templates/jwiki_view.html
    88 }}}
    89 
    90 It first analyzes the Jinja2 control structures, and tries to provide some helpful diagnostics in case of nesting or stylistic errors.
    91 It also adds curly braces to the statements, so if you have an editor which can do matching of brace pairs, you can quickly spot the origin o a nesting problem.
    92 
    93 Finally, while the indentation of the statements is free in Jinja2 templates, being consistent with it also helps to ensure a proper nesting.
    94 {{{
    95 # -- Jinja2 check for 'trac/wiki/templates/jwiki_view.html'
    96    12    EXTENDS "jlayout.html"
    97    18          {BLOCK title
    98    19            {IF title:
    99    21            }IF
    100    22          }BLOCK title
    101    25        {BLOCK head
    102    26          SET modify_perm = 'WIKI_MODIFY' in perm(page.resource)
    103    27          SET is_not_latest = page.exists and page.version != latest_version
    104    31          {IF version or page.author == 'trac':
    105    33          }IF
    106    34          {IF modify_perm:
    107    40          }IF
    108    52        }BLOCK head
    109    56        {BLOCK content
    110    57          SET modify_perm = 'WIKI_MODIFY' in perm(page.resource)
    111    58          SET create_perm = 'WIKI_CREATE' in perm(page.resource)
    112    59          SET admin_perm = 'WIKI_ADMIN' in perm(page.resource)
    113    60          SET is_not_latest = page.exists and page.version != latest_version
    114    63          {IF version:
    115    67                {WITH
    116    68                  SET version = page.version
    117    69                  SET author = authorinfo(page.author)
    118    70                  SET date = pretty_dateinfo(page.time)
    119    71                  SET hef = href.wiki(page.name, action='diff', version=page.version)
    120    72                  {TRANS version, author, date, href
    121    77                  }TRANS
    122    78                }WITH
    123    84          }IF
    124    87            {IF page.exists:
    125    91              SET last_modification = (page.comment and
    126    97              {IF not version:
    127   100                {WITH
    128   101                  SET href = href.wiki(page.name, action='diff',
    129   103                  SET date = pretty_dateinfo(page.time)
    130   104                  {TRANS href, last_modification, date
    131   109                  }TRANS
    132   110                }WITH
    133   116              }IF
    134   117            ELSE:
    135   119              {TRANS name = name_of(page.resource)
    136   124              }TRANS
    137   126            }IF
    138   129          {WITH
    139   130            SET alist = attachments
    140   131            SET compact = True
    141   132            SET foldable = True
    142   133            INCLUDE "jlist_of_attachments.html"
    143   134          }WITH
    144   136          {WITH
    145   137            SET delete_perm = 'WIKI_DELETE' in perm(page.resource)
    146   138            SET rename_perm = 'WIKI_RENAME' in perm(page.resource)
    147   139            {IF modify_perm or create_perm or delete_perm:
    148   141              {IF modify_perm or create_perm:
    149   145                {IF is_not_latest and modify_perm:
    150   148                ELIF page.exists and modify_perm:
    151   151                ELIF not page.exists and create_perm:
    152   154                  {IF templates:
    153   161                    {FOR t in sorted(templates):
    154   165                    }FOR
    155   168                  }IF
    156   169                }IF
    157   173              {IF page.exists:
    158   174                {WITH alist = attachments
    159   175                  INCLUDE "jattach_file_form.html"
    160   176                }WITH
    161   177              }IF
    162   178            }IF
    163   180            {IF page.exists and rename_perm:
    164   187            }IF
    165   188            {IF page.exists and delete_perm:
    166   193                {IF page.version == latest_version:
    167   195                }IF
    168   199            }IF
    169   201          }IF
    170   202          }WITH
    171   204          {IF not page.exists and higher:
    172   208              {FOR markup in higher:
    173   210              }FOR
    174   213          }IF
    175   215          {IF not page.exists and related:
    176   219              {FOR markup in related:
    177   221              }FOR
    178   224          }IF
    179   230        }BLOCK content
    180 # -- Jinja2 OK
    181 }}}
    182 
    183 As a second step, jinjachecker removes the Jinja2 markup and performs a validation of the document, using lxml.
    184 {{{
    185 # -- HTML check for 'trac/wiki/templates/jwiki_view.html'
    186     1
    187 ...
    188    13
    189    14 <!DOCTYPE html>
    190    15 <html>
    191    16   <head>
    192    17     <title>
    193    18
    194    19
    195    20       ${title} ${ super() }
    196    21
    197    22
    198    23     </title>
    199    24
    200    25
    201    26
    202    27
    203    28
    204    29     ${ super() }
    205    30
    206 ...
    207    52
    208    53   </head>
    209    54
    210    55   <body>
    211    56
    212    57
    213    58
    214    59
    215    60
    216    61     <div id="content" class="${classes('wiki', create=not page.exists)}">
    217    62
    218 ...
    219   213
    220   214
    221   215     </div>
    222   216
    223   217     ${ super() }
    224   218
    225   219
    226   220   </body>
    227   221 </html>
    228 # -- HTML OK
    229 }}}
     80In order to facilitate the creation of error-free Jinja2 templates for HTML (or XML), we also wrote a little utility called [PortingFromGenshiToJinja/Checker jinjachecker], which will help troubleshoot the most common nesting problems.