Sympa TT2 guidelines ?

This is an exemple of template found in the current sympa codebase.

It is very similar to the examples you can find in the Template Toolkit documentation. However, i think we can write better code using some extra features.

This article is not a tutorial: it assumes you're already familiar with the TT2 syntax.

so let's see

[% 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 %] 
  • As filters can be used to anything you have to render, [%|loc%]Create list[%END%] may be rewritten as [% "Create list" |loc %].
  • The SET instruction is optionnal and can use ternary operator. so
[% IF action == 'create_list_request' %]
   [% SET class = 'MainMenuLinksCurrentPage' %]
[% ELSE %]
   [% SET class = 'MainMenuLinks' %]
[% END %]

can be rewritten as

[% class = action == 'create_list_request'
    ? 'MainMenuLinksCurrentPage'
    : 'MainMenuLinks';
%]

closing a template section is not needed as all the TT2 instructions can be separated with ;. so

[% IF may_create_list %]
[% class = action == 'create_list_request'
    ? 'MainMenuLinksCurrentPage'
    : 'MainMenuLinks';
%]
[% END %]

can be written as

[% IF may_create_list;
    class = action == 'create_list_request'
    ? 'MainMenuLinksCurrentPage'
    : 'MainMenuLinks';
END %]

Another thing to realize is that void context strings wherever you are in template instructions and can be interpolated so this

   %]<li><a class="[% class %]" href="[% path_cgi %]/create_list_request">[%
   "Create list" | loc;
   %]</a></li>[%

can be written as

   "<li><a class="$class" href="$class/create_list_request">[%
        "Create list" | loc
    %]</a></li>";

or even

   "<li><a class="$class" href="$class/create_list_request">";
   "Create list" | loc;
   "</a></li>";

Put it all together and the final block is

[% 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 %]

more examples to come

  • more about block, include, filters and local variables to create more reusable business objects into sympa.
  • about vmethods to be faster and less memory consumming
  • ... (just ask ...)