| 1 | package Catalyst::View::EmbeddedPerl::PerRequest::ValiantRole; | ||
| 2 | |||
| 3 | 1 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Valiant::HTML::Util::Form; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Valiant::HTML::Util::Pager; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | our $VERSION = '0.001007'; | |
| 8 | 1 | eval $VERSION; | |
| 9 | |||
| 10 | # https://metacpan.org/pod/Valiant::HTML::Util::TagBuilder | ||
| 11 | 1 | my @TAG_BUILDER = qw( tags tag content_tag join_tags text link_to ); | |
| 12 | |||
| 13 | # https://metacpan.org/pod/Valiant::HTML::Util::FormTags | ||
| 14 | 1 | my @FORM_TAGS = qw( field_value field_id field_name button_tag checkbox_tag fieldset_tag | |
| 15 | legend_tag form_tag label_tag radio_button_tag option_tag text_area_tag | ||
| 16 | input_tag password_tag hidden_tag submit_tag select_tag options_for_select | ||
| 17 | options_from_collection_for_select ); | ||
| 18 | |||
| 19 | # https://metacpan.org/pod/Valiant::HTML::Util::Form | ||
| 20 | 1 | my @FORM = qw( form_for fields_for form_with fields ); | |
| 21 | |||
| 22 | # https://metacpan.org/pod/Valiant::HTML::Util::Pager | ||
| 23 | 1 | my @PAGER = qw( pager_for ); | |
| 24 | |||
| 25 | 1 | has _form_helpers => ( | |
| 26 | is => 'ro', | ||
| 27 | required => 1, | ||
| 28 | lazy => 1, | ||
| 29 | handles => [@TAG_BUILDER, @FORM_TAGS, @FORM], | ||
| 30 | builder => '_build_form_helpers'); | ||
| 31 | |||
| 32 | 1 | ▶ | sub _build_form_helpers { |
|
Subroutine
_build_form_helpers: called CC=1 SCAR=0.0
| |||
| 33 | 1 | my $self = shift; | |
| 34 | 1 | return Valiant::HTML::Util::Form->new( | |
| 35 | view => $self, | ||
| 36 | context => $self->ctx, | ||
| 37 | controller => $self->ctx->controller); | ||
| 38 | } | ||
| 39 | |||
| 40 | 1 | has _pager_helpers => ( | |
| 41 | is => 'ro', | ||
| 42 | required => 1, | ||
| 43 | lazy => 1, | ||
| 44 | handles => [@PAGER], | ||
| 45 | builder => '_build_pager_helpers'); | ||
| 46 | |||
| 47 | 0 | ▶ | sub _build_pager_helpers { |
|
Subroutine
_build_pager_helpers: not called CC=1 SCAR=6.9
| |||
| 48 | 0 | my $self = shift; | |
| 49 | 0 | return Valiant::HTML::Util::Pager->new( | |
| 50 | view => $self, | ||
| 51 | context => $self->ctx, | ||
| 52 | controller => $self->ctx->controller); | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | 3 | ▶ | sub _generate_html_helpers { |
|
Subroutine
_generate_html_helpers: called CC=1 SCAR=0.0
| |||
| 57 | 3 | my $self = shift; | |
| 58 | return my @helpers = map { | ||
| 59 | 3 | my $helper_name = $_; | |
| 60 | 1 | ▶ | $helper_name => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 61 | 1 | my ($self, $c, @args) = @_; | |
| 62 | 1 | return $self->$helper_name(@args); | |
| 63 | 90 | }; | |
| 64 | } (@TAG_BUILDER, @FORM_TAGS, @FORM, @PAGER); | ||
| 65 | } | ||
| 66 | |||
| 67 | 3 | ▶ | around 'default_helpers' => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 68 | 3 | my ($orig, $self, @args) = @_; | |
| 69 | return ( | ||
| 70 | 3 | $self->$orig(@args), | |
| 71 | $self->_generate_html_helpers(), | ||
| 72 | ); | ||
| 73 | 1 | }; | |
| 74 | |||
| 75 | 3 | ▶ | around 'modify_temple_args' => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 76 | 3 | my ($orig, $self, @args) = @_; | |
| 77 | 3 | my %args = $self->$orig(@args); | |
| 78 | 3 | return ( %args, auto_escape => 1 ); | |
| 79 | 1 | }; | |
| 80 | |||
| 81 | 1 | 1; | |
| 82 | |||
| 83 | =head1 NAME | ||
| 84 | |||
| 85 | Catalyst::View::EmbeddedPerl::PerRequest::ValiantRole - Add Valiant Formbuilder methods | ||
| 86 | |||
| 87 | =head1 SYNOPSIS | ||
| 88 | |||
| 89 | Declare a view in your Catalyst application: | ||
| 90 | |||
| 91 | package Example::View::Hello; | ||
| 92 | |||
| 93 | use Moose; | ||
| 94 | extends 'Catalyst::View::EmbeddedPerl::PerRequest'; | ||
| 95 | with 'Catalyst::View::EmbeddedPerl::PerRequest::ValiantRole'; | ||
| 96 | |||
| 97 | has 'name' => (is => 'ro', isa => 'Str'); | ||
| 98 | |||
| 99 | __PACKAGE__->config(prepend => 'use v5.40', content_type=>'text/html; charset=UTF-8'); | ||
| 100 | __PACKAGE__->meta->make_immutable; | ||
| 101 | |||
| 102 | __DATA__ | ||
| 103 | %= form_for('person', sub ($self, $fb, $model) { | ||
| 104 | %= $fb->input('name'); | ||
| 105 | %= $fb->input('age'); | ||
| 106 | % }); | ||
| 107 | |||
| 108 | Produces the following output: | ||
| 109 | |||
| 110 | <form accept-charset="UTF-8" enctype="application/x-www-form-urlencoded" method="post"> | ||
| 111 | <input id="person_name" name="person.name" type="text" value=""/> | ||
| 112 | <input id="person_age" name="person.age" type="text" value=""/> | ||
| 113 | </form> | ||
| 114 | |||
| 115 | =head1 DESCRIPTION | ||
| 116 | |||
| 117 | This is just a role that proxies methods from L<Valiant::HTML::Util::Form> into you | ||
| 118 | instance of L<Catalyst::View::EmbeddedPerl::PerRequest>. These are used to create | ||
| 119 | HTML form elements. All methods are also exposed as helpers directly into your | ||
| 120 | template. | ||
| 121 | |||
| 122 | Since L<Valiant::HTML::Util::Form> inherits from L<Valiant::HTML::Util::FormTags>, | ||
| 123 | which inherits from L<Valiant::HTML::Util::TagBuilder>, this adds quite a few methods | ||
| 124 | to your namespace: | ||
| 125 | |||
| 126 | From L<Valiant::HTML::Util::TagBuilder> | ||
| 127 | |||
| 128 | tags tag content_tag join_tags text link_to | ||
| 129 | |||
| 130 | From L<Valiant::HTML::Util::FormTags> | ||
| 131 | |||
| 132 | field_value field_id field_name button_tag checkbox_tag fieldset_tag | ||
| 133 | legend_tag form_tag label_tag radio_button_tag option_tag text_area_tag | ||
| 134 | input_tag password_tag hidden_tag submit_tag select_tag options_for_select | ||
| 135 | options_from_collection_for_select | ||
| 136 | |||
| 137 | From L<Valiant::HTML::Util::Form> | ||
| 138 | |||
| 139 | form_for fields_for form_with fields | ||
| 140 | |||
| 141 | You will also have all the methods that are public for L<Catalyst::View::EmbeddedPerl::PerRequest>. | ||
| 142 | |||
| 143 | =head1 HTML ESCAPING | ||
| 144 | |||
| 145 | By default the view will escape all output to prevent cross site scripting attacks. | ||
| 146 | If you want to output raw HTML you can use the C<raw> helper. For example: | ||
| 147 | |||
| 148 | <%= raw $self->html %> | ||
| 149 | |||
| 150 | See L<Template::EmbeddedPerl::SafeString> for more information. | ||
| 151 | |||
| 152 | You can disable this feature by setting the C<auto_escape> option to false in the | ||
| 153 | view configuration. For example if you are not using this to generate HTML output | ||
| 154 | you might not want it. | ||
| 155 | |||
| 156 | =head1 COOKBOOK | ||
| 157 | |||
| 158 | Ideas for using this module. | ||
| 159 | |||
| 160 | =head2 Creating a base view | ||
| 161 | |||
| 162 | Given you will need to make a lot of view classes (at least one class per template) you | ||
| 163 | might be well off to create a common base class: | ||
| 164 | |||
| 165 | package Example::View::HTML; | ||
| 166 | |||
| 167 | use Moose; | ||
| 168 | |||
| 169 | extends 'Catalyst::View::EmbeddedPerl::PerRequest'; | ||
| 170 | with 'Catalyst::View::EmbeddedPerl::PerRequest::ValiantRole'; | ||
| 171 | |||
| 172 | __PACKAGE__->meta->make_immutable; | ||
| 173 | __PACKAGE__->config( | ||
| 174 | prepend => 'use v5.40', | ||
| 175 | content_type=>'text/html; charset=UTF-8' | ||
| 176 | ); | ||
| 177 | |||
| 178 | Used like this: | ||
| 179 | |||
| 180 | package Example::View::Hello; | ||
| 181 | |||
| 182 | use Moose; | ||
| 183 | extends 'Example::View::HTML'; | ||
| 184 | |||
| 185 | has 'name' => (is => 'ro', isa => 'Str'); | ||
| 186 | |||
| 187 | __PACKAGE__->meta->make_immutable; | ||
| 188 | __DATA__ | ||
| 189 | %= form_for('person', sub ($self, $fb, $model) { | ||
| 190 | %= $fb->input('name'); | ||
| 191 | %= $fb->input('age'); | ||
| 192 | % }); | ||
| 193 | |||
| 194 | |||
| 195 | =head1 SEE ALSO | ||
| 196 | |||
| 197 | Related reading | ||
| 198 | |||
| 199 | =over 4 | ||
| 200 | |||
| 201 | =item * L<Catalyst> | ||
| 202 | |||
| 203 | The Catalyst web framework. | ||
| 204 | |||
| 205 | =item * L<Catalyst::View::BasePerRequest> | ||
| 206 | |||
| 207 | The base class for per-request views in Catalyst. | ||
| 208 | |||
| 209 | =item * L<Template::EmbeddedPerl> | ||
| 210 | |||
| 211 | Module used for processing embedded Perl templates. | ||
| 212 | |||
| 213 | =item * L<Catalyst::View::EmbeddedPerl> | ||
| 214 | |||
| 215 | The view for which this role applies | ||
| 216 | |||
| 217 | =back | ||
| 218 | |||
| 219 | =head1 AUTHOR | ||
| 220 | |||
| 221 | John Napiorkowski C<< <jjnapiork@cpan.org> >> | ||
| 222 | |||
| 223 | =head1 LICENSE | ||
| 224 | |||
| 225 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 226 | |||
| 227 | =cut | ||