New template base method create().
[gruta.git] / Gruta / Template / BASE.pm
blob7a979bd7c7ddf09a23178f70449d6a2c4498f68d
1 package Gruta::Template::BASE;
3 use Carp;
5 sub templates {
6 my $self = shift;
8 my @r = ( );
10 foreach my $p (split(':', $self->{path})) {
11 if (opendir D, $p) {
12 while (my $l = readdir D) {
13 next if -d $p . '/' . $l;
14 push @r, $l;
17 closedir D;
21 return sort @r;
25 sub _assert {
26 my $self = shift;
27 my $template_id = shift;
29 if (not $template_id =~ /^[-\w\d-]+$/) {
30 croak "Invalid template '$template_id'";
33 return $self;
37 sub template {
38 my $self = shift;
39 my $template_id = shift;
41 $self->_assert($template_id);
43 my $content = undef;
45 foreach my $p (split(':', $self->{path})) {
46 if (open F, $p . '/'. $template_id) {
47 $content = join('', <F>);
48 close F;
50 last;
54 return $content;
58 sub save_template {
59 my $self = shift;
60 my $template_id = shift;
61 my $content = shift;
63 $self->_assert($template_id);
65 # only can be saved on the first directory
66 my ($p) = (split(':', $self->{path}))[0];
68 open F, '>' . $p . '/' . $template_id
69 or croak "Can't write template '${p}/${template_id}'";
71 print F $content;
72 close F;
76 sub create {
77 my $self = shift;
79 # create first directory
80 my ($p1) = (split(':', $self->{path}))[0];
82 mkdir $p1, 0755;