-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtt2.html
75 lines (68 loc) · 4.82 KB
/
tt2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html><html><head><title>Sympa TT2 guidelines ?</title><meta charset="utf-8"><meta http-equiv="x-ua-compatible" content="ie=edge"><meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<link rel="stylesheet" href="../../look.css"><link rel="stylesheet" href="http://django-static.u-strasbg.fr/dipstrap/latest/css/bootstrap-unistra.min.css"><link rel="stylesheet" href="http://django-static.u-strasbg.fr/dipstrap/latest/css/bootstrap-theme-unistra.min.css"><link rel="stylesheet" href="http://django-static.u-strasbg.fr/dipstrap/latest/css/main.css"></head><body><div role="navigation" class="navbar navbar-default navbar-fixed-top"><img src="../../img/unistra-white.png" alt="unistra" class="pull-left"><div class="container"><a href="#" class="navbar-brand"></a></div></div><div style="margin-top: 3em" class="container-fluid"><div class="row-fluid"><div class="col-md-2">
</div><div class="row-fluid"></div></div><div class="col-md-10"><div class="col-md-10"><h1>Sympa TT2 guidelines ?</h1><p>This is an exemple of template found in the current <a href="http://www.sympa.org">sympa</a> codebase.</p>
<p>It is very similar to the examples you can find in the <a href="https://metacpan.org/release/Template-Toolkit">Template Toolkit documentation</a>. However, i think we can write better code using some extra features.</p>
<p>This article is not a tutorial: it assumes you're already familiar with the TT2 syntax.</p>
<p>so let's see</p>
<pre class="tt2"><code>[% IF may_create_list %]
[% IF action == 'create_list_request' %]
[% SET class = 'MainMenuLinksCurrentPage' %]
[% ELSE %]
[% SET class = 'MainMenuLinks' %]
[% END %]<li><a class="[% class %]" href="[%
path_cgi %]/create_list_request" >[%|loc%]Create list[%END%]</a></li>
[% END %] </code></pre>
<ul>
<li>As filters can be used to anything you have to render, <code>[%|loc%]Create list[%END%]</code> may be rewritten as <code>[% "Create list" |loc %]</code>.</li>
<li>The <code>SET</code> instruction is optionnal and can use ternary operator. so</li>
</ul>
<pre class="tt2"><code>[% IF action == 'create_list_request' %]
[% SET class = 'MainMenuLinksCurrentPage' %]
[% ELSE %]
[% SET class = 'MainMenuLinks' %]
[% END %]</code></pre>
<p>can be rewritten as</p>
<pre class="tt2"><code>[% class = action == 'create_list_request'
? 'MainMenuLinksCurrentPage'
: 'MainMenuLinks';
%]</code></pre>
<p>closing a template section is not needed as all the TT2 instructions can be separated with <code>;</code>. so</p>
<pre class="tt2"><code>[% IF may_create_list %]
[% class = action == 'create_list_request'
? 'MainMenuLinksCurrentPage'
: 'MainMenuLinks';
%]
[% END %]</code></pre>
<p>can be written as</p>
<pre class="tt2"><code>[% IF may_create_list;
class = action == 'create_list_request'
? 'MainMenuLinksCurrentPage'
: 'MainMenuLinks';
END %]</code></pre>
<p>Another thing to realize is that void context strings wherever you are in template instructions and can be interpolated so this</p>
<pre class="tt2"><code> %]<li><a class="[% class %]" href="[% path_cgi %]/create_list_request">[%
"Create list" | loc;
%]</a></li>[%</code></pre>
<p>can be written as</p>
<pre class="tt2"><code> "<li><a class="$class" href="$class/create_list_request">[%
"Create list" | loc
%]</a></li>";</code></pre>
<p>or even</p>
<pre class="tt2"><code> "<li><a class="$class" href="$class/create_list_request">";
"Create list" | loc;
"</a></li>";</code></pre>
<p>Put it all together and the final block is</p>
<pre class="tt2"><code>[% IF may_create_list;
class = action == 'create_list_request'
? 'MainMenuLinksCurrentPage'
: 'MainMenuLinks';
"<li><a class="$class" href="$class/create_list_request">";
"Create list" | loc;
"</a></li>";
END %]</code></pre>
<p>more examples to come</p>
<ul>
<li>more about block, include, filters and local variables to create more reusable business objects into sympa.</li>
<li>about vmethods to be faster and less memory consumming</li>
<li>... (just ask ...)</li>
</ul></div></div></div></div></body><script src="../../behave.js"></script><script src="js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script><script src="http://django-static.u-strasbg.fr/dipstrap/latest/js/vendor/jquery-1.11.0.min.js"></script><script src="http://django-static.u-strasbg.fr/dipstrap/latest/js/vendor/bootstrap.min.js"></script></html>