f06b81b0e776fcef114ad98f1db72853ac771ad4
[catagits/CatalystX-HelpText.git] / lib / CatalystX / HelpText / ViewRole.pm
1 package CatalystX::HelpText::ViewRole;
2 use Moose::Role;
3 use Try::Tiny;
4 use Moose::Autobox;
5 use namespace::autoclean;
6
7 requires 'expose_methods';
8
9 around expose_methods => sub {
10     my ($orig, $self, @args) = @_;
11     my $m = $self->$orig(@args) || [];
12     [ $m->flatten, 'help_text' ];
13 };
14
15 sub help_text {
16     my ($self, $c, $key) = @_;
17     try {
18         return $c->model('Help')->get_help_text_for($c, $key);
19     }
20     catch {
21         $c->log->warn("Error retrieving help_text: ".$_);
22         return '';
23     };
24 }
25
26 1;
27
28 =head1 NAME
29
30 CatalystX::HelpText::ViewRole - Role to be applied to Views
31
32 =head1 SYNOPSIS
33
34   package MyApp::View::HTML;
35   use Moose;
36
37   extends 'Catalyst::View::TT';
38   with 'CatalystX::HelpText::ViewRole';
39
40   ... then, in your template code ...
41
42   [% helptext('SomeHelpTopic') %]
43
44 =cut
45