| 1 | package Template::Lace::ComponentCallback; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Template::Lace::DOM; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | sub new { return bless pop, shift } | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 8 | 0 | ▶ | sub cb { return shift->{cb} } | ||||||||
|
Subroutine
cb: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 9 | 1 | ▶ | sub make_dom { return Template::Lace::DOM->new(pop) } | ||||||||
|
Subroutine
make_dom: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 10 | 1 | ▶ | sub model_class { return ref(shift) } | ||||||||
|
Subroutine
model_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 11 | |||||||||||
| 12 | sub create { | ||||||||||
| 13 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
create: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 14 | 1 | return bless +{ cb => $self, @_ }, ref($self); | |||||||||
| 15 | } | ||||||||||
| 16 | |||||||||||
| 17 | sub get_processed_dom { | ||||||||||
| 18 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get_processed_dom: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 19 | 1 | local $_ = $self; | |||||||||
| 20 | 1 | local %_ = %$self; | |||||||||
| 21 | 1 | my $response = $self->{cb}->($self, %$self); | |||||||||
| 22 | 1 | ▶ | return ref($response) ? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | $response : | ||||||||||
| 24 | $self->make_dom($response); | ||||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 1 | 1; | |||||||||
| 28 | |||||||||||
| 29 | =head1 NAME | ||||||||||
| 30 | |||||||||||
| 31 | Template::Lace::ComponentCallback - Create a component easily from a coderef | ||||||||||
| 32 | |||||||||||
| 33 | =head1 SYNOPSIS | ||||||||||
| 34 | |||||||||||
| 35 | { | ||||||||||
| 36 | package Local::Template::User; | ||||||||||
| 37 | |||||||||||
| 38 | use Moo; | ||||||||||
| 39 | with 'Template::Lace::ModelRole'; | ||||||||||
| 40 | |||||||||||
| 41 | has [qw/title story/] => (is=>'ro', required=>1); | ||||||||||
| 42 | |||||||||||
| 43 | sub template {q[ | ||||||||||
| 44 | <html> | ||||||||||
| 45 | <head> | ||||||||||
| 46 | <title></title> | ||||||||||
| 47 | </head> | ||||||||||
| 48 | <body> | ||||||||||
| 49 | <div id='story'></div> | ||||||||||
| 50 | <tag-anchor href='more.html' target='_top'> | ||||||||||
| 51 | See More | ||||||||||
| 52 | </tag-anchor> | ||||||||||
| 53 | </body> | ||||||||||
| 54 | </html> | ||||||||||
| 55 | ]} | ||||||||||
| 56 | |||||||||||
| 57 | sub process_dom { | ||||||||||
| 58 | my ($self, $dom) = @_; | ||||||||||
| 59 | $dom->title($self->title) | ||||||||||
| 60 | ->at('#story') | ||||||||||
| 61 | ->content($self->story); | ||||||||||
| 62 | } | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | use Template::Lace::ComponentCallback; | ||||||||||
| 66 | my $factory = Template::Lace::Factory->new( | ||||||||||
| 67 | model_class=>'Local::Template::User', | ||||||||||
| 68 | component_handlers=>+{ | ||||||||||
| 69 | tag => { | ||||||||||
| 70 | anchor => Template::Lace::ComponentCallback=>new(sub { | ||||||||||
| 71 | my ($self, %attrs) = @_; | ||||||||||
| 72 | return "<a href='$_{href}' target='$_{target}'>$_{content}</a>"; | ||||||||||
| 73 | }), | ||||||||||
| 74 | }, | ||||||||||
| 75 | }, | ||||||||||
| 76 | ); | ||||||||||
| 77 | |||||||||||
| 78 | In this case C<%attrs> are the results of processing attributes. | ||||||||||
| 79 | |||||||||||
| 80 | B<NOTE> You might prefer to call this via L<Template::Lace::Utils> instead. | ||||||||||
| 81 | |||||||||||
| 82 | =head1 DESCRIPTION | ||||||||||
| 83 | |||||||||||
| 84 | Lets you make quick and dirty components from a coderef. To make this even | ||||||||||
| 85 | faster and dirtier we localize $_ to $self and %_ to %attrs. | ||||||||||
| 86 | |||||||||||
| 87 | =head1 METHODS | ||||||||||
| 88 | |||||||||||
| 89 | This class defines the following public instance methods: | ||||||||||
| 90 | |||||||||||
| 91 | sub make_dom | ||||||||||
| 92 | |||||||||||
| 93 | Create an instance of L<Template::Lace::DOM>. Useful if you have complex | ||||||||||
| 94 | component setup and transformation. | ||||||||||
| 95 | |||||||||||
| 96 | =head1 SEE ALSO | ||||||||||
| 97 | |||||||||||
| 98 | L<Template::Lace>. | ||||||||||
| 99 | |||||||||||
| 100 | =head1 AUTHOR | ||||||||||
| 101 | |||||||||||
| 102 | Please See L<Template::Lace> for authorship and contributor information. | ||||||||||
| 103 | |||||||||||
| 104 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 105 | |||||||||||
| 106 | Please see L<Template::Lace> for copyright and license information. | ||||||||||
| 107 | |||||||||||
| 108 | =cut | ||||||||||