Edgewall Software

Changes between Version 21 and Version 22 of TracDev/PortingFromGenshiToJinja


Ignore:
Timestamp:
Mar 24, 2016, 8:49:02 PM (8 years ago)
Author:
Christian Boos
Comment:

extends, xml, …

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/PortingFromGenshiToJinja

    v21 v22  
    7070See [http://jinja.pocoo.org/docs/dev/extensions/#with-statement with] doc.
    7171
    72 Caveat: note how the boolean constants slightly differ from Python, `False` becomes `false`, `True` becomes `true` and `None` is `none`.
     72In passing, note how the boolean constants slightly differ from Python, `False` becomes `false`, `True` becomes `true` and `None` is `none`.
     73
     74Caveat: contrary to the Genshi way, one shouldn't include the `"layout.html"` template in order to inherit the default page layout. There's a big difference in the way template "inheritance" works between Genshi and Jinja2, see HtmlTemplates#Jinjaarchitecture for all the details.
     75
     76Despite these differences, we kept the same spirit and there's actually also a `"layout.html"` template that you can "inherit" from (as well as a `"theme.html"` template that the layout inherits in turn). But instead of "including" it in your template, you "extend" it:
     77{{{#!html+jinja
     78# extends "layout.html"
     79}}}
     80But this is not the place to go further in the details about how extending works, refer to the [http://jinja.pocoo.org/docs/dev/templates/#extends extends] documentation and to the link above for how this applies to Trac.
     81
    7382
    7483=== simple if...then (no else)   #if
     
    424433More specifically, you'll have to "extend" the [source:cboos.git/trac/templates/jlayout.html@jinja2 jlayout.html] template, and redefine the "head", and "content" blocks if needed.
    425434
    426 All the details are available in Proposals/Jinja#Jinja2theme, including a walkthrough for the specific example of the [source:cboos.git/trac/search/templates/jsearch.html@jinja2 jsearch.html] template.
     435All the details are available in HtmlTemplates#Jinjaarchitecture, including a walkthrough for the specific example of the [source:cboos.git/trac/search/templates/jsearch.html@jinja2 jsearch.html] template.
    427436
    428437For the jsearch.html example we focus on the //structure// of the templates, the //include// relationship and the decomposition in //blocks//.
     
    698707You can then use the `tag` builder API regardless of its origin.
    699708
     709
     710The behavior of the new `tag` builder is nearly the same as the old one,
     711except that it has even more "knowledge" about the HTML format. For example, for the `class` attribute (or rather, `class_` as `class` is a reserved Python keyword), and for the `style` attribute, dicts can be given as parameters instead of plain strings. Other attributes, like `checked`, will be omitted when given a `False` value.
     712
     713As this special behavior could be unwanted when arbitrary XML must be generated instead of XHTML, another builder, `xml`, is now available. The `xml` builder can be used the same way as the `tag` builder, but when serialized, its only special behavior is to omit attributes which have the value `None`.
     714
     715==== The `Markup` class
     716
    700717Likewise, if you want to use the `Markup` class, you should write:
    701718{{{#!python
     
    704721In "old" versions of Trac, you'll get `genshi.core.Markup`, whereas now you'll get `markupsafe.Markup`: as we're using Jinja2, we're also making use of its direct dependency [PyPI:MarkupSafe].
    705722
     723==== The `escape` function
     724
    706725{{{#!python
    707726from trac.util.html import escape