2005-12-11

Template Toolkit 入门

来源: 本站收集整理 作者:佚名 评论 0 条
 

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

参考

  • Introduction to Template Toolkit (part 1)
  • Introduction to Template Toolkit (part 2)
  • Introduction to Template Toolkit (part 3)

(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:Template Toolkit 的配置选项  
下一篇:我对 Catalyst 的理解和介绍
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号