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) ?> |
| 117 | We 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)"> |
| 123 | We have ${count > 10 and 'too much' or count} elements. |
| 124 | </py:with> |
| 125 | }}} |
| 126 | Note that we had to use `>` in Genshi, instead of directly `>` as in Clearsilver. |
| 127 | |
| 128 | ==== Examples ==== |
| 129 | Let'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 | {{{ |
| 212 | |
| 213 | Another example from Trac, a bit more complex. |
| 214 | This 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 |
| 219 | def: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 | |