Edgewall Software

Changes between Version 4 and Version 5 of TracDev/ApiChanges/0.11


Ignore:
Timestamp:
Sep 13, 2006, 3:44:01 PM (18 years ago)
Author:
Christian Boos
Comment:

Added set/py:with and def sections, as well as a new, more complex, example

Legend:

Unmodified
Added
Removed
Modified
  • TracDev/ApiChanges/0.11

    v4 v5  
    9393   }}}
    9494
    95 
    96 Let's take a full-contained example from the Trac source, the simple index.cs / index.html templates.
    97 
    98 [source:trunk/templates/index.cs@3725 index.cs]:
    99 {{{
     95==== define a macro ====
     96 - Clearsilver
     97   {{{
     98#!xml
     99<?cs def:entry(key, val)?>
     100 <dt><?cs var:key ?></dt><dd><?cs var:val ?></dd>
     101<?cs /def ?>
     102   }}}
     103 - Genshi
     104   {{{
     105#!xml
     106<py:def function="entry(key, val='--')">
     107 <dt>$key</dt><dd>$val</dd>
     108</py:def>
     109   }}}
     110   As you can see, with Genshi it's also easy to specify default values for the macro arguments.
     111
     112==== set a variable ====
     113 - Clearsilver
     114   {{{
     115#!xml
     116<?cs set:count = len(collection) ?>
     117We have <?cs if:count > 10 ?>too much<?cs else ?><?cs var:count ?><?cs /if ?> elements.
     118   }}}
     119 - Genshi
     120   {{{
     121#!xml
     122<py:with vars="count = len(collection)">
     123We have ${count &gt; 10 and 'too much' or count} elements.
     124</py:with>
     125   }}}
     126   Note that we had to use `&gt;` in Genshi, instead of directly `>` as in Clearsilver.
     127
     128==== Examples ====
     129Let's first take a simple full-contained example from the Trac source, the simple index.cs / index.html templates.
     130
     131 - Clearsilver [source:trunk/templates/index.cs@3725 index.cs]:
     132   {{{
    100133#!xml
    101134<!DOCTYPE html
     
    116149 /each ?></ul></body>
    117150</html>
    118 }}}
    119 
    120 [source:sandbox/genshi/templates/index.html@3728 index.html]:
    121 {{{
     151   }}}
     152 - Genshi [source:sandbox/genshi/templates/index.html@3728 index.html]:
     153   {{{
    122154#!xml
    123155<!DOCTYPE html
     
    144176  </body>
    145177</html>
    146 }}}
     178   }}}
    147179
    148180Some remarks:
     
    178210Of course, the great benefit of this constraint is that you'll end up quite naturally with well-formed content, which was far from being a trivial achievement using Clearsilver templates. Granted, you could still insert directly some non well-formed `Markup` data in your template, but again, if you use the [http://genshi.edgewall.org/wiki/Documentation/builder.html genshi.builder] ''tag'' facility for this, that's hardly a risk.
    179211
     212
     213Another example from Trac, a bit more complex.
     214This illustrates how to use `<py:def>` and `<py:with>`, to convert a Clearsilver macro using `<?cs def: ?>` and `<?cs set: ?>`.
     215
     216 - Clearsilver
     217   {{{
     218#!xml
     219def:browser_path_links(path, file) ?><?cs
     220<?cs set:first = #1 ?><?cs
     221  each:part = path ?><?cs
     222   set:last = name(part) == len(path) - #1 ?><a<?cs
     223   if:first ?> class="first" title="Go to root directory"<?cs
     224    set:first = #0 ?><?cs
     225   else ?> title="View <?cs var:part.name ?>"<?cs
     226   /if ?> href="<?cs var:part.href ?>"><?cs var:part.name ?></a><?cs
     227   if:!last ?><span class="sep">/</span><?cs /if ?><?cs
     228 /each ?><?cs
     229/def ?><?cs
     230   }}}
     231 - Genshi
     232   {{{
     233#!xml
     234  <py:def function="browser_path_links(path_links)">
     235    <py:for each="idx, part in enumerate(path_links)">
     236      <py:with vars="first = (idx == 0); last = (idx == len(path_links) - 1)">
     237        <a class="${first and 'first' or None}"
     238          title="${first and 'Go to root directory' or 'View ' + part.name}"
     239          href="$part.href">$part.name</a>
     240        <py:if test="not last"><span class="sep">/</span></py:if>
     241      </py:with>
     242    </py:for>
     243  </py:def>
     244   }}}
     245
     246
    180247=== Changes in the controllers ===
    181248