| 1 | package Template::Lace::Model::AutoTemplate; | ||
| 2 | |||
| 3 | use Moo::Role; | ||
| 4 | use File::Spec; | ||
| 5 | |||
| 6 | sub get_path_to_template { | ||
| 7 | my ($class) = @_; | ||
| 8 | my @parts = split("::", $class); | ||
| 9 | my $filename = lc(pop @parts); | ||
| 10 | my $path = "$class.pm"; | ||
| 11 | $path =~s/::/\//g; | ||
| 12 | my $inc = $INC{$path}; | ||
| 13 | my $base = $inc; | ||
| 14 | $base =~s/$path$//g; | ||
| 15 | return my $template_path = File::Spec->catfile($base, @parts, $filename.'.html'); | ||
| 16 | } | ||
| 17 | |||
| 18 | sub slurp_template_from { | ||
| 19 | my ($class, $template_path) = @_; | ||
| 20 | open(my $fh, '<', $template_path) | ||
| 21 | || die "can't open '$template_path': $@"; | ||
| 22 | local $/; my $slurped = $fh->getline; | ||
| 23 | close($fh); | ||
| 24 | return $slurped; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub template { | ||
| 28 | my ($class, @args) = @_; | ||
| 29 | my $template_path = $class->get_path_to_template; | ||
| 30 | return $class->slurp_template_from($template_path); | ||
| 31 | } | ||
| 32 | |||
| 33 | 1; | ||
| 34 | |||
| 35 | =head1 NAME | ||
| 36 | |||
| 37 | Template::Lace::Model::AutoTemplate - More easily find your template | ||
| 38 | |||
| 39 | =head1 SYNOPSIS | ||
| 40 | |||
| 41 | Create a template class at path $HOME/lib/MyApp/Template/List.pm | ||
| 42 | |||
| 43 | package MyApp::Template::List; | ||
| 44 | |||
| 45 | use Moo; | ||
| 46 | with 'Template::Lace::Model::AutoTemplate'; | ||
| 47 | |||
| 48 | has 'items' => (is=>'ro', required=>1); | ||
| 49 | |||
| 50 | sub process_dom { | ||
| 51 | my ($self, $dom) = @_; | ||
| 52 | $dom->ol('#todos'=>$self->items); | ||
| 53 | } | ||
| 54 | |||
| 55 | Also create an HTML file like this at $HOME/lib/MyApp/Template/list.html | ||
| 56 | |||
| 57 | <html> | ||
| 58 | <head> | ||
| 59 | <title>Things To Do</title> | ||
| 60 | </head> | ||
| 61 | <body> | ||
| 62 | <ol id='todos'> | ||
| 63 | <li>What To Do?</li> | ||
| 64 | </ol> | ||
| 65 | </body> | ||
| 66 | </html> | ||
| 67 | |||
| 68 | When you create an factory of the template class, we automatically load the | ||
| 69 | template from the file and make it available for running transformations. | ||
| 70 | |||
| 71 | =head1 DESCRIPTION | ||
| 72 | |||
| 73 | By default we look for a string returned from a method c<template> in your class | ||
| 74 | to provide the source for your generated HTML. This can be handy for small templates | ||
| 75 | but often when a template is larger or when you have an HTML designer you prefer to | ||
| 76 | have your templates in a stand alone file. This role will first check if you are | ||
| 77 | returning something from C<template> method and if not it will look for a file in the | ||
| 78 | same directory as the template class with a name based on the class. If it finds the | ||
| 79 | file it with use that as your template. | ||
| 80 | |||
| 81 | The convention for the filename of the HTML version of the template is to take the | ||
| 82 | class file name, replace the '.pm' with '.html' and lowercase the name. For example | ||
| 83 | if you have a class 'MyApp::Templates::User', at '$HOME/lib/MyApp/Templates/User.pm' | ||
| 84 | we'd expect to find a file template at '$HOME/lib/MyApp/Templates/user.html'. | ||
| 85 | |||
| 86 | Since its generally not great practice to rely on mixed case alone to distinguish | ||
| 87 | your filenames this convention should be acceptable. Your results may vary. You can | ||
| 88 | override the method 'get_path_to_template' if you prefer a different lookup | ||
| 89 | convention (or even hardcode a particular path). | ||
| 90 | |||
| 91 | =head1 SEE ALSO | ||
| 92 | |||
| 93 | L<Template::Lace>. | ||
| 94 | |||
| 95 | =head1 AUTHOR | ||
| 96 | |||
| 97 | Please See L<Template::Lace> for authorship and contributor information. | ||
| 98 | |||
| 99 | =head1 COPYRIGHT & LICENSE | ||
| 100 | |||
| 101 | Please see L<Template::Lace> for copyright and license information. | ||
| 102 | |||
| 103 | =cut |