Commit | Line | Data |
c8c4f056 |
1 | package CatalystX::HelpText::ViewRole; |
2 | use Moose::Role; |
3 | use Try::Tiny; |
030fd7f2 |
4 | use Moose::Autobox; |
5 | use namespace::autoclean; |
c8c4f056 |
6 | |
7 | requires 'expose_methods'; |
8 | |
9 | around expose_methods => sub { |
10 | my ($orig, $self, @args) = @_; |
030fd7f2 |
11 | my $m = $self->$orig(@args) || []; |
12 | [ $m->flatten, 'help_text' ]; |
c8c4f056 |
13 | }; |
14 | |
72bb1615 |
15 | has helptext_model => ( |
16 | is => 'ro', |
17 | default => 'Help', |
18 | ); |
19 | |
c8c4f056 |
20 | sub help_text { |
21 | my ($self, $c, $key) = @_; |
72bb1615 |
22 | my $model = try { $c->model($self->helptext_model) }; |
23 | if (!$model) { |
24 | $c->log->warn(sprintf("Cannot find the '%s' model for %s", $self->helptext_model, ref($self))) if $c->debug; |
25 | return; |
26 | } |
27 | if (!$model->can('get_help_text_for')) { |
28 | $c->log->warn(sprintf("Your '%s' model used by '%s' does not have a get_help_text_for method", $self->helptext_model, ref($self))) if $c->debug; |
29 | return; |
30 | } |
c8c4f056 |
31 | try { |
72bb1615 |
32 | $model->get_help_text_for($key); |
c8c4f056 |
33 | } |
34 | catch { |
72bb1615 |
35 | $c->log->warn("Error retrieving help_text key '$key' from model: $_") |
36 | if $c->debug; |
c8c4f056 |
37 | return ''; |
38 | }; |
39 | } |
40 | |
41 | 1; |
42 | |
43 | =head1 NAME |
44 | |
45 | CatalystX::HelpText::ViewRole - Role to be applied to Views |
46 | |
47 | =head1 SYNOPSIS |
48 | |
49 | package MyApp::View::HTML; |
50 | use Moose; |
51 | |
52 | extends 'Catalyst::View::TT'; |
53 | with 'CatalystX::HelpText::ViewRole'; |
54 | |
55 | ... then, in your template code ... |
56 | |
57 | [% helptext('SomeHelpTopic') %] |
58 | |
be01cec8 |
59 | =head1 NOTES |
c8c4f056 |
60 | |
be01cec8 |
61 | Should work with any view which implements the C<expose_methods> functionality in the same manor |
62 | that L<Catalyst::View::TT> does, however this has only been tried with that view. |
63 | |
64 | =head1 SEE ALSO |
65 | |
66 | =over |
67 | |
68 | =item L<CatalystX::HelpText> |
69 | |
70 | =item L<Catalyst::View::TT/expose_methods> |
71 | |
72 | =back |
73 | |
74 | =head1 AUTHOR |
75 | |
76 | Toomas Doran, C<< t0m at state51.co.uk >> |
77 | |
78 | Cinxgler Mariaca Minda, C<< cinxgler at ci-info.com >> |
79 | |
80 | =head1 COPYRIGHT |
81 | |
82 | Copyright Oscar Music and Media 2011. |
83 | |
84 | =head1 LICENSE |
85 | |
86 | This sofware is free software, and is licensed under the same terms as perl itself. |
87 | |
88 | =cut |