# lib/Blog/Controller/Root.pm package Blog::Controller::Root; use strict; use warnings; use base 'Catalyst::Controller::BindLex'; __PACKAGE__->config->{namespace} = ''; sub all_posts : Path : Args(0) { my ($self, $c) = @_; my @posts : Stashed = $c->model('DBIC::Posts')->search( {}, { order_by => 'posted DESC', rows => 5, page => 1, } ); } sub end : ActionClass('RenderView') { } #### # lib/Blog/View/TD/Main.pm package Blog::View::TD::Main; use strict; use warnings; use Template::Declare::Tags; sub wrapper(&) { my $content = shift; smart_tag_wrapper { html { head { title { c->stash->{title}; }; }; body { h1 { c->stash->{title} }; $content->(); } } } } template all_posts => sub { c->stash->{title} = 'Blog posts'; wrapper { foreach my $post (@{c->stash->posts||[]}) { div { attr { class => 'post' }; h2 { $post->title }; p { 'Written by ' . $post->author}; }; div { attr { class => 'body'}; outs_raw($post->body); # bad } } } }; 1; __END__ #### # lib/Blog/Model/DBIC.pm package Blog::Model::DBIC; use strict; use base 'Catalyst::Model::DBIC::Schema'; __PACKAGE__->config( schema_class => 'Blog::Schema', connect_info => [ 'DBI:SQLite:root/catalyst', ], ); 1; #### # Create Schema perl script/blog_create.pl model DBIC DBIC::Schema Blog::Schema create=static DBI:SQLite:root/catalyst # Create View perl script/blog_create.pl view TD Template::Declare #### # Edit Makefile.PL $ diff Makefile.PL~ Makefile.PL 5a6,7 > requires 'Catalyst::Model::DBIC::Schema'; > requires 'Catalyst::View::Template::Declare'; perl Makefile.PL sudo make installdeps sudo make test #### $ perl script/blog_server.pl -r -d [debug] Debug messages enabled [debug] Loaded plugins: .----------------------------------------------------. | Catalyst::Plugin::ConfigLoader 0.18 | | Catalyst::Plugin::Static::Simple 0.20 | '----------------------------------------------------' [debug] Loaded dispatcher "Catalyst::Dispatcher" [debug] Loaded engine "Catalyst::Engine::HTTP::Restarter" [debug] Found home "/home/foo/test/perl/catalyst/blog/Blog" [debug] Loaded Config "/home/foo/test/perl/catalyst/blog/Blog/blog.yml" [info] Loading subtemplate Blog::View::TD::Main [debug] Loaded components: .-----------------------------------------+----------. | Class | Type | +-----------------------------------------+----------+ | Blog::Controller::Root | instance | | Blog::Model::DBIC | instance | | Blog::View::TD | instance | | Blog::View::TD::Main | class | '-----------------------------------------+----------' [debug] Loaded Private actions: .----------------------+------------------------+--------------. | Private | Class | Method | +----------------------+------------------------+--------------+ | /end | Blog::Controller::Root | end | | /all_posts | Blog::Controller::Root | all_posts | '----------------------+------------------------+--------------' [debug] Loaded Path actions: .-------------------------------------+-------------. | Path | Private | +-------------------------------------+-------------+ | / | /all_posts | '-------------------------------------+-------------' [info] Blog powered by Catalyst 5.7011 You can connect to your server at http://localhost:3000 #### [info] *** Request 1 (0.027/s) [17489] [Mon Oct 22 15:29:38 2007] *** [debug] "GET" request for "/" from "127.0.0.1" [debug] Path is "/" [error] Caught exception in Blog::Controller::Root->all_posts "Can't call method "search" on an undefined value at /home/foo/test/perl/catalyst/blog/Blog/script/../lib/Blog/Controller/Root.pm line 11." [info] Request took 0.239138s (4.182/s) .----------------------------------------+-----------. | Action | Time | +----------------------------------------+-----------+ | /all_posts | 0.000361s | | /end | 0.002197s | '----------------------------------------+-----------' #### $ sqlite3 root/database SQLite version 3.3.6 Enter ".help" for instructions sqlite> .tables posts sqlite> .schema posts CREATE TABLE posts (id integer primary key, title text not null, body text not null, author text not null, posted date not null); sqlite> select * from posts; 1|test|a new post|Foo|2007-01-01 00:01 2|another test|another post|Bar|2007-01-01 00:02 sqlite>