PRE/POST_CHOMP
我在
Template Toolkit 的配置选项 写过可以设置 POST_CHOMP 和 PRE_CHOMP 为 1 来去掉
TT 代码所带来的前后空行。不过如果你不想设置这个的话也是可以去掉前后空行的。比如这样:
Hello [% a = 3 %]
World [% a %]
这个输出的话为“Hello \nWorld 3\n”。而这么写可以把里面的 \n 去掉:
Hello [% a = 3 -%]
World [% a -%]
输出为“Hello World 3”。这个在
http://www.stonehenge.com/merlyn/LinuxMag/col60.html 里提到过。
合并两个 TT 代码
[% user.name %][% END %]
可以写成
[% user.name; END %]
INSERT 和 INCLUDE 的区别
INSERT 只是插入文件的内容而不管你是不是另一个 .tt 文件。而 INCLUDE 的话如果是另一个 .tt 文件的话会执行它。
比如你有一个文件叫 footer.tt, 内容为:
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> [% CatalystVersion %].
当在另一个文件中调用 [% INSERT footer.tt %] 时输出的结果跟上面的会一样。里面的 [% CatalystVersion %] 是原封不动。而如果是用 [% INCLUDE footer.tt %] 的话里面的 [% CatalystVersion %] 会被执行为这个变量的值。
BLOCK
比如你有段代码要执行两次。比如我有一个导航栏,需要上面放一个下面放一个。那我可以这么写:
[% show_guidebar = BLOCK %]
<p>
[% IF prev_topic %]<<Previous: <a href="[% prev_topic.cms_file %].html">[% prev_topic.cms_title %]</a>[% END %]
[% IF prev_topic and next_topic %] [% END %]
[% IF next_topic %]>>Next: <a href="[% next_topic.cms_file %].html">[% next_topic.cms_title %]</a>[% END %]
</p>
[% END %][% show_guidebar %]
...
[% show_guidebar %]
注释
注释很简单,在 [% 后加上 # 那这一行就被注释掉了。唯一值得注意的是下面这两种是不一样的:
[%# a = 77
b = 88
%]
a: [% a %] b: [% b %]
输出 a: b:
而[% 和 # 中间空了一格后就只有注释一行而不是整个 [% %], 如:
[% # a = 77
b = 88
%]
a: [% a %] b: [% b %]
输出 a: b: 88
参考