文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>第四章 掌握View 第二节 TT入门

第四章 掌握View 第二节 TT入门

时间:2007-04-17  来源:xiaoshengcaicai

Template::Toolkit, 简称TT, 它本身是一套非常强大的模块,跟Catalyst是无关的,所以这一节是只讲TT的基本用法,下一节讲如何在Catalyst里面使用TT.

一  关于TT
TT它是一套模板处理系统, 它的主要功能简单来讲就是,给它一个模板以及模板内变量的值,它可以帮你把变量的值替换进模板,生成最后的文本. 常用于生成HTML代码.
关于TT更详细的文档,请参看CPAN.

二 TT基本用法

1. 替换变量:

use Template;
my $tt = Template->new;

#要替换的变量值:
my $vars = {name => 'xiaosheng'};

#模板可以是一个文件, 也可以是一段字符串, 也可以是一个文件句柄,

#方法1
my $template_str = 'hi, my name is [% name %]';
$tt->process(\$template_str, $vars) || die $tt->error(), "\n";
#方法2
my $template_file = 'a.tt';  #a.tt的内容是'hi, my name is [% name %]'
$tt->process($template_file , $vars) || die $tt->error(), "\n";

#方法3

my $template_str = 'hi, my name is [% name %]';
my $output;
$tt->process(\$template_str, $vars, \$output) || die $tt->error(), "\n";


在上面的例子里,方法1,跟方法2都会往标准输出打印出'hi, my name is xiaosheng'

方法3里面, $output的值为字符串'hi, my name is xiaosheng';

2 循环语句

use Template;
my $tt = Template->new;

#例2.1
my $vars = {
  cai_list=> ['caicai1', 'caicai2', 'caicai3']
};
my $template_str = '
[% FOREACH cai IN cai_list %]
the name is [% cai %]
[% END %]
';
$tt->process(\$template_str, $vars) || die $tt->error(), "\n";

#打印出
the name is caicai1

the name is caicai2

the name is caicai3
#例2.2
$vars = {
  cai_list=> [
      { name => 'caicai1', id => 1 },
      { name => 'caicai2',id => 2 }
  ]
};
$template_str = '
[% FOREACH cai IN cai_list %]
the name is [% cai.name %], the id is [% cai.id %]
[% END %]
';
$tt->process(\$template_str, $vars) || die $tt->error(), "\n";
#打印出
the name is caicai1, the id is 1

the name is caicai2, the id is 2

#例2.3
$template_str = '
[% i = 10 %]
[% WHILE i > 0 %]
now is i=[% i %]
[% i = i - 1 %]
[% END %]
';

$tt->process(\$template_str, $vars) || die $tt->error(), "\n";
#打印出
now is i=3
now is i=2
now is i=1

3 条件语句

 my $vars = {
  cai_list=> [
      { name => 'caicai1', score => 100 },
      { name => 'caicai2', score => 85  },
      { name => 'caicai3', score => 95 },
      { name => 'caicai1', score => 75 },
      { name => 'caicai2', score => 65  },
      { name => 'caicai3', score => 55 },
  ]
};
my $template_str = '
[% FOREACH cai IN cai_list %]
    [% IF cai.score == 100  %]
         [% cai.name %] : 满分
    [% ELSIF cai.score >= 90 %]
         [% cai.name %] : 优秀
    [% ELSIF cai.score >= 80 %]
         [% cai.name %] : 良好
    [% ELSIF cai.score >= 70 %]
         [% cai.name %] : 一般
    [% ELSIF cai.score >= 60 %]
         [% cai.name %] : 及格               
    [% ELSE %]
        [% cai.name %] : 不及格
    [% END %]
[% END %]
';

$tt->process(\$template_str, $vars) || die $tt->error(), "\n";
#打印出
caicai1 : 满分
caicai2 : 良好
caicai3 : 优秀
caicai1 : 一般
caicai2 : 及格
caicai3 : 不及格

4 去空格以及回车
use Template;
my $tt = Template->new;
#例4.1
my $vars = {
  name => 'xiaosheng',
};
my $template_str = '
hi,
[% name %],
good morning
';
$tt->process(\$template_str, $vars) || die $tt->error(), "\n";

#打印出
hi,
xiaosheng
,good morning

#例4.2
$vars = {
name => 'xiaosheng',
};

$template_str = '
hi,
[%- name %]
,good morning
';
$tt->process(\$template_str, $vars) || die $tt->error(), "\n";

#打印出
hi,xiaosheng
,good morning


#例4.3
$vars = {
name => 'xiaosheng',
};

$template_str = '
hi,
[% name -%]
,good morning
';
$tt->process(\$template_str, $vars) || die $tt->error(), "\n";

#打印出
hi,
xiaosheng,good morning

#例4.4
$vars = {
name => 'xiaosheng',
};

$template_str = '
hi,
[%- name -%]
,good morning
';
$tt->process(\$template_str, $vars) || die $tt->error(), "\n";
#打印出
hi,xiaosheng,good morning

5 常用的虚方法(virtual method)
在上面例子我们看到,对于替换进模板的那些变量,
如果$var1是一个hashref, 如果在TT里面要得到$var1->{key1}的值, 在TT里面可以写var1.key1.
如果$var1是一个对象,它有一个方法名为print,在TT里面要调用$var1->print()的话就得写成可以通过var1.print.
也就是说, 在TT里面通过 . 可以调用一个对象的方法.

如果var1是一个scalar或者是一个list又或者是hash, 它本身没有var1->defined这个方法, 但是在TT里面你通过var1.defined来判断var1是否是defined的值, 这个defined的方法,是TT给这个变量加上的虚方法.
关于详细的virtual method,请参考CPAN文档http://search.cpan.org/~abw/Template-Toolkit-2.18/lib/Template/Manual/VMethods.pod

这里稍微介绍几个虚方法:

use Template;
my $tt = Template->new;
#defined
#适用于scalar,判断一个scalar是否defined
my $vars = {
  cai_list=> [
      { name => 'caicai1' },
      { name => '0' },
      { name => undef }
  ]
};
my $template_str = '
[% FOREACH cai IN cai_list %]
     [% IF cai.name.defined %]
        [% cai.name %]
     [% ELSE %]
        not defined
     [% END %]  
[% END %]
';
$tt->process(\$template_str, $vars) || die $tt->error(), "\n";

#打印出
caicai1
0
not defined


#length
#适用于scalar,得到一个字符串含有的字符个数
my $vars = {
cai_list=> [
{ name => 'caicai1' },
{ name => 'xiaosheng' },
]
};
my $template_str = '
[% FOREACH cai IN cai_list %]
length of name is [% cai.name.length %]
[% END %]
';$tt->process(\$template_str, $vars) || die $tt->error(), "\n";

#打印出
length of name is 7
length of name is 9


#size
#适用于list,得到list包含的元素个数
my $vars = {
cai_list=> [
{ name => 'caicai1' },
{ name => 'xiaosheng' },
]
};
my $template_str = '
[% cai_list.size %]
';
$tt->process(\$template_str, $vars) || die $tt->error(), "\n";

#打印出
2

6 过滤器filter
对于要替换进模板的变量,在显示之前可以给它加上一个或者多个过滤器, 把过滤后的结果再替换到模板里面.
关于所有的filter,请参考http://search.cpan.org/~abw/Template-Toolkit-2.18/lib/Template/Manual/Filters.pod
这里稍微介绍几个
use Template;
my $tt = Template->new;
#html
#起到html escape的作用
my $vars = {
  var => '<font color=red>xiaosheng</font>',
};
my $template_str = '
[% var | html %]
';
$tt->process(\$template_str, $vars) || die $tt->error(), "\n";

#打印出
&lt;font color=red&gt;xiaosheng&lt;/font&gt;

#format
#格式化输出
my $vars = {
var => '100.345',
};
my $template_str = '
[% var | format("%.02f")%]
';
$tt->process(\$template_str, $vars) || die $tt->error(), "\n";

#打印出
100.35
相关阅读 更多 +
排行榜 更多 +
方块枪战战场安卓版

方块枪战战场安卓版

飞行射击 下载
战斗火力射击安卓版

战斗火力射击安卓版

飞行射击 下载
空中防御战安卓版

空中防御战安卓版

飞行射击 下载