Chinaunix首页 | 论坛 | 博客
  • 博客访问: 387772
  • 博文数量: 69
  • 博客积分: 1984
  • 博客等级: 上尉
  • 技术积分: 953
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-28 00:43
个人简介

学无所长,一事无成

文章分类

全部博文(69)

文章存档

2015年(19)

2014年(14)

2013年(9)

2012年(17)

2010年(10)

我的朋友

分类:

2010-08-18 15:34:32

Catalyst::Manual::Tutorial::02_CatalystBasics
学习笔记 (2010-08-18 星期三)
 
文章来源:
 
例子程序:
 
安装:cpan Task::Catalyst::Tutorial

一、
创建 app,运行:
$ catalyst.pl Hello
$ script/hello_server.pl -r 
 
二、
 

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;                  
    # Hello World     

    $c->response->body( $c->welcome_message );
}

在 Catalyst 的新版本中,default index 不建议使用 :Private 。

:Path 表示匹配所有 url ,因为 Path 后面没有跟任何路径。

:Args(0) 表示只接受不带任何参数的 url 。

三、添加 action 

sub hello :Global {
    my ( $self, $c ) = @_;    
    $c->response->body("Hello, World!");
}

    
    [debug] Loaded Path actions:
    .-------------------------------------+--------------------------------------.
    | Path                                | Private                              |
    +-------------------------------------+--------------------------------------+
    | /                                   | /index                               |
    | /                                   | /default                             |
    | /hello                              | /hello                               |
    '-------------------------------------+--------------------------------------'
    ...
 

四、 创建 view

$ script/hello_create.pl view TT TT

查看文件: lib/Hello/View/TT.pm ,你会发现仅仅做了些配置,将扩展设置为 .tt。

学习 TT 的完整用法可以参考: ,我们这里只学简单的。

1、创建 root/hello.tt 文件,内容如下,这里 template.name 表示 hello.tt ,

你可以根据需要写入 perl 变量:

<p>
    This is a TT view template, called '[% template.name %]'.
</p>


2、修改 lib/Hello/Controller/Root.pm  如下:

sub hello :Global {
    my ( $self, $c ) = @_;    
    $c->stash(template => 'hello.tt');
}

stash 的不同写法,全部有效(上面的写法流行):

  • $c->stash->{template} = 'hello.tt';
  • $c->stash(template => 'hello.tt', foo => 'bar',
                  another_thing => 1);
  • $c->stash({template => 'hello.tt', foo => 'bar',
                  another_thing => 1});

五、

1、$ script/hello_create.pl controller Site

查看文件:lib/Hello/Controller/Site.pm

sub test :Local {
    my ( $self, $c ) = @_;

    $c->stash(username => 'John',
              template => 'site/test.tt');
}


Local 指明该 action 响应 url:  "controller/method",这里是site/test;

关于action 的更多知识:

2、

这里我们其实不用指定 template , TT 会自动匹配 "controller/method.tt" 模板。

编辑:root/site/test.tt,内容如下:

<p>Hello, [% username %]!</p>


 

 

本章结束

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