CREATE TABLE `notes` (
`note_id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255),
`message` TEXT,
`modify_time` timestamp NOT NULL,
`create_time` timestamp NOT NULL,
`creator` INTEGER UNSIGNED NOT NULL,
`private` TINYINT NOT NULL DEFAULT 0,
PRIMARY KEY(`note_id`)
)
ENGINE=MyISAM;
CREATE TABLE `users` (
`user_id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`password` VARCHAR(255),
PRIMARY KEY(`user_id`)
)
ENGINE=MyISAM;
1 package Note::DBI;
2
3 use Note::Constant;
4 use base 'Class::DBI';
5
6 my $database = Note::Constant::DATABASE;
7 my $user = Note::Constant::DB_USER;
8 my $pwd = Note::Constant::DB_PWD;
9
10 __PACKAGE__->set_db('Main', "dbi:mysql:$database:localhost", $user, $pwd);
11
12 1;
13
1 package Note::User;
2
3 use Class::DBI::Pager;
4
5 use base 'Note::DBI';
6
7 __PACKAGE__->table('users');
8 __PACKAGE__->columns(All => qw/user_id name password/);
9 __PACKAGE__->columns(Essential => qw/user_id name password/);
10
11 __PACKAGE__->has_many(notes => 'Note::Note', {sort => "create_time desc"} );
1 package Note::Note;
2
3 use lib './';
4 use EasyDateTime;
5 use Class::DBI::Pager;
6
7 use base 'Note::DBI';
8
9 __PACKAGE__->table('notes');
10 __PACKAGE__->columns(All => qw/note_id title message modify_time create_time creator private/);
11 __PACKAGE__->columns(Essential => qw/note_id title message create_time creator private/);
12
13 __PACKAGE__->has_a(creator => "Note::User");
14 __PACKAGE__->has_a(create_time => 'EasyDateTime',
15 inflate => 'new',
16 deflate => sub {shift->str("%datetime")},
17 );
18
19 __PACKAGE__->set_sql(new_list => "select * from __TABLE__ order by create_time desc limit 10");
20
21 1;
# get me
my ($user) = Note::User->search(name => "Wu.Hao");
print $user->name; # Wu.Hao
# get all notes created by me
my @notes = $user->notes();
# get all notes whose title include "mail"
my @notes = Note::Note->search_like(title => "%mail%");
# want to change name
$user->name("Go.Ko");
$user->update();
GET
[% variable %]
[% hash.key %]
[% list.n %]
[% code(args) %]
[% obj.meth(args) %]
[% "value: $var" %]SET
[% variable = other_variable
variable = 'literal text @ $100'
variable = "interpolated text: $var"
list = [ val, val, val, val, ... ]
list = [ val..val ]
hash = { var => val, var => val, ... }
%]FOREACH
[% FOREACH variable = [ val, val, val ] %] # either
[% FOREACH variable = list %] # or
[% FOREACH list %] # or
content...
[% variable %]
[% END %]WHILE
[% WHILE condition %]
content
[% END %]IF...ELSE...
[% IF condition %]
content
[% ELSIF condition %]
content
[% ELSE %]
content
[% END %]SWITCH
[% SWITCH variable %]
[% CASE val1 %]
content
[% CASE [ val2, val3 ] %]
content
[% CASE %] # or [% CASE DEFAULT %]
content
[% END %]
1 #!perl
2
3 use lib '../lib';
4 use Template;
5 use Note::DBI;
6 use Note::Note;
7
8 my @notes = Note::Note->retrieve_all();
9 my $tt = new Template();
10 $tt->process(\*DATA, {notes => \@notes});
11
12 __DATA__
13 [% FOREACH note = notes %]
14 [% note.creator.name %]([% note.create_time.str('%yyyy-%MM-%dd %hh:%mm') %]) : [% note.title %]
15 [% END %]
Result:
Wu.Hao(2005-08-01 15:12) : test
Wu.hao(2005-08-01 15:13) : test2
Guest(2005-08-01 15:14) : testA
Guest(2005-08-01 15:15) : testB
my $r = [Table 1
{ a => 1, b => 2, c => 3,},
{ a => 2, b => 4, c => 6,}
];
A Value | B Value | C Value |
1 | 2 | 3 |
2 | 4 | 6 |
#!perl
use Template;
my $r = [
{ a => 1, b => 2, c => 3,},
{ a => 2, b => 4, c => 6,}
];
my $tt = new Template();
$tt->process(
\*DATA,
{
r => $r,
}
)
or die $tt->error;
__DATA__
<table border=1>
<tr>
<td>A Value</td><td>B Value</td><td>C Value</td>
</tr>
[% FOREACH r %]
<tr>
<td>[% a %]</td><td>[% b %]</td><td>[% c %]</td>
</tr>
[% END %]
</table>
#!perl
use HTML::Template;
my $r = [
{ a => 1, b => 2, c => 3,},
{ a => 2, b => 4, c => 6,}
];
my $tmpl = new HTML::Template(filehandle => *DATA);
$tmpl->param(r => $r);
print $tmpl->output();
__DATA__
<table border=1>
<tr>
<td>A Value</td><td>B Value</td><td>C Value</td>
</tr>
<TMPL_LOOP r>
<tr>
<td><TMPL_VAR a></td><td><TMPL_VAR b></td><td><TMPL_VAR c></td>
</tr>
</TMPL_LOOP>
</table>
Table 2
A Value | C Value | B Value |
1 | 3 | 2 |
2 | 6 | 4 |
Table 3
A Value | C Value |
1 | 3 |
2 | 6 |
Table 2
$r = [
{
row => [ { value => 1, col => 'a' }, { value => 3, col => 'c' }, { value => 2, col => 'b' } ]
},
{
row => [ { value => 2, col => 'a' }, { value => 6, col => 'c' }, { value => 4, col => 'b' } ]
},
];
#!perlTable 3
use Template;
my $r = [
{ a => 1, b => 2, c => 3,},
{ a => 2, b => 4, c => 6,}
];
my $c = [qw/a c b/];
my $tt = new Template();
$tt->process(
\*DATA,
{
r => $r,
c => $c,
value => sub { return $_[0]->{ $_[1] } }
}
)
or die $tt->error;
__DATA__
<table border=1>
<tr>
[% FOREACH j = c %]
<td>
[% SWITCH j %]
[% CASE 'a' %]A Value
[% CASE 'b' %]B Value
[% CASE 'c' %]C Value
[% END %]
</td>
[% END %]
</tr>
[% FOREACH i = r %]
<tr>
[% FOREACH j = c %]
<td>[% value(i,j) %]</td>
[% END %]
</tr>
[% END %]
</table>
my $c = [qw/a c/];