Chinaunix首页 | 论坛 | 博客
  • 博客访问: 300139
  • 博文数量: 106
  • 博客积分: 1948
  • 博客等级: 上尉
  • 技术积分: 947
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-27 00:11
文章分类

全部博文(106)

文章存档

2014年(1)

2013年(14)

2012年(61)

2011年(30)

分类: 系统运维

2012-01-25 18:28:28


先建立一个视图(View)

  1. script/myapp_create.pl view test TT
  2.  exists "/var/www/myApp/script/../lib/myApp/View"
  3.  exists "/var/www/myApp/script/../t"
  4. created "/var/www/myApp/script/../lib/myApp/View/test.pm"
  5. created "/var/www/myApp/script/../t/view_test.t"

我的第一个例子
利用视图打印hello world t

先改写控制器(Controller)
vi lib/myApp/Controller/test.pm
  1. sub view : Local {
  2.     my ( $self, $c ) = @_;
  3.     $c->stash->{user} = "t";           #给t.tt传的值
  4.     $c->stash->{template} = "t.tt";    #用t.tt这个模版
  5. }
再写一个模版,默认路径是myApp/root/base/下
mkdir root/base
vi root/base/t.tt

  1. hello world
  2. [% user %]     #这行就是接受控制器传来的那个值
浏览器输入
hello world t


补充一下
如果不喜欢模版名字叫.tt可以编辑
vi lib/myApp/View/test.pm

  1. package myApp::View::test;

  2. use strict;
  3. use warnings;

  4. use base 'Catalyst::View::TT';

  5. __PACKAGE__->config(
  6. #    TEMPLATE_EXTENSION => '.tt',     #默认的
  7.     TEMPLATE_EXTENSION => '.html',    #修改后的
  8.     render_die => 1,
  9.     DEBUG=>'all',                     #开启DEBUG信息
  10.     INCLUDE_PATH=>[ myApp->path_to('root','template'),],  #修改默认模版路径到root/template了
  11. );
  12. 1;
这样模版就是.html文件了

再做一个模版文件
vi root/template/t.html
[% str %]

控制器修改成
  1. sub view1 : Local {
  2.     my ( $self, $c ) = @_;
  3.     $c->stash->{str} = "hello world";
  4.     $c->stash->{template} = "t.html";
  5. }

我们看看有调试信息的打印结果
浏览器输入1
## t.html line 1 : [% str %] ## hello world


阅读(756) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~