File Coverage

lib/Mojolicious/Plugin/Fondation/Action/Templates.pm
Criterion Covered Total %
statement 74 74 100.0
branch 11 12 91.6
condition 8 15 53.3
subroutine 7 7 100.0
pod 0 1 0.0
total 100 109 91.7


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Fondation::Action::Templates;
2             $Mojolicious::Plugin::Fondation::Action::Templates::VERSION = '0.02';
3             # ABSTRACT: Registers templates and zones from plugin share directories
4              
5 13     13   70 use Mojo::Base 'Mojolicious::Plugin::Fondation::Action::Base', -signatures;
  13         24  
  13         83  
6              
7 13     13   1362 use Mojolicious::Plugin::Fondation::Utils qw(share_relative);
  13         22  
  13         11976  
8              
9 161     161 0 232 sub after_load ($self, $long, $conf, $share_dir) {
  161         210  
  161         267  
  161         205  
  161         200  
  161         206  
10 161 100 66     550 return unless $share_dir && -d $share_dir;
11              
12 139         4481 my $templates_dir = $share_dir->child('templates');
13 139 100       2713 return unless -d $templates_dir;
14              
15 87         1427 my $manager = $self->manager;
16 87         416 my $app = $manager->app;
17 87         361 my $short = $manager->registry->{$long}{short_name};
18              
19             # Add templates directory (priority: "earlier in load_order = higher priority")
20 87         354 push @{$app->renderer->paths}, $templates_dir->to_string;
  87         292  
21 87         1347 $self->log->debug("Added templates path: " . share_relative($templates_dir));
22              
23             # Register templates in the registry
24 87         1304 $self->_register_templates($long, $short, $templates_dir);
25 87         2294 $self->_register_zones($long, $short, $templates_dir);
26             }
27              
28 87     87   146 sub _register_templates ($self, $long, $short, $templates_dir) {
  87         117  
  87         143  
  87         119  
  87         125  
  87         105  
29 87         173 my $manager = $self->manager;
30 87   50     347 my $entry = $manager->registry->{$long} //= {};
31              
32 87   50     731 $entry->{templates} //= {};
33              
34 87         158 my @found_templates = ();
35              
36 183     183   26202 $templates_dir->list_tree({ dir => 0 })->each(sub ($file, $idx) {
  183         270  
  183         251  
  183         242  
37             # Accept .html.ep, .ep, .txt.ep, etc.
38 183 100       624 return unless $file->basename =~ /\.(?:html?|txt|xml|json)\.ep$/i;
39              
40 171         8399 my $rel_path = $file->to_rel($templates_dir)->to_string;
41 171         14324 my $template_name = $rel_path;# =~ s/\.ep$//r;
42              
43 171   50     341 $entry->{templates}{$template_name} = {
44             full_path => $file->to_string,
45             rel_path => $rel_path,
46             basename => $file->basename,
47             last_modified => $file->stat->mtime // 0,
48             };
49              
50 171         32513 push @found_templates, $template_name;
51 171         550 $self->log->debug("Registered template: $template_name");
52 87         438 });
53              
54             }
55              
56 87     87   152 sub _register_zones ($self, $long, $short, $templates_dir) {
  87         126  
  87         152  
  87         115  
  87         126  
  87         107  
57 87         314 my $zone_base = $templates_dir->child('zones');
58 87 100       1636 return unless -d $zone_base;
59              
60 18         396 my $manager = $self->manager;
61 18         88 my $entry = $manager->registry->{$long};
62              
63 18   50     166 $entry->{zones} //= {};
64              
65 30     30   6880 $zone_base->list_tree({ dir => 0 })->each(sub ($file, $idx) {
  30         53  
  30         37  
  30         37  
66 30         83 my $basename = $file->basename;
67 30 50       1273 return unless $basename =~ /\.(html|js)\.ep$/i;
68              
69 30         90 my $type = lc $1;
70 30         97 my $rel = Mojo::Path->new($file->to_rel($templates_dir)->to_string)->to_string;
71 30         6050 my ($zone) = $rel =~ m{^zones/$type/(.+)/[^/]+$};
72              
73 30   50     195 $entry->{zones}{$type} //= {};
74 30   50     143 $entry->{zones}{$type}{$zone} //= [];
75              
76             # HTML: store template name for render_to_string
77             # JS: pre-slurp content (no render needed)
78 30 100       75 if ($type eq 'html') {
79 18         65 my $template_name = $rel =~ s/\.html\.ep$//r;
80 18         30 push @{$entry->{zones}{$type}{$zone}}, $template_name;
  18         55  
81             }
82             else {
83 12         21 push @{$entry->{zones}{$type}{$zone}}, $file->slurp;
  12         74  
84             }
85              
86 30         1275 $self->log->debug("Registered zone: $rel ($type/$zone)");
87 18         67 });
88             }
89              
90             1;
91              
92             __END__