| 1 | package Catalyst::View::Valiant::HTMLBuilder; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use Moo::_Utils; | ||
| 5 | use Module::Runtime; | ||
| 6 | use Valiant::HTML::SafeString; | ||
| 7 | use Valiant::HTML::Util::TagBuilder; | ||
| 8 | use Valiant::JSON::Util; | ||
| 9 | use Scalar::Util; | ||
| 10 | use Sub::Util; | ||
| 11 | use URI::Escape (); | ||
| 12 | use Carp; | ||
| 13 | |||
| 14 | extends 'Catalyst::View::BasePerRequest'; | ||
| 15 | |||
| 16 | has 'caller' => (is=>'ro', required=>0, predicate=>'has_caller'); | ||
| 17 | |||
| 18 | has 'tb' => (is=>'ro', required=>1, lazy=>1, builder=>'_build_tags'); | ||
| 19 | |||
| 20 | sub _build_tags { | ||
| 21 | my $self = shift; | ||
| 22 | return Valiant::HTML::Util::TagBuilder->new(view=>$self); | ||
| 23 | } | ||
| 24 | |||
| 25 | has 'view_fragment' => (is=>'ro', predicate=>'has_view_fragment'); | ||
| 26 | |||
| 27 | sub components { return qw/Form Pager/ } | ||
| 28 | |||
| 29 | foreach my $component (components()) { | ||
| 30 | my $component_method_name = lc($component); | ||
| 31 | my $sub = Sub::Util::set_subname $component_method_name => sub { | ||
| 32 | my $self = shift; | ||
| 33 | return $self->{"__${component_method_name}"} ||= do { | ||
| 34 | my $module = Module::Runtime::use_module("Catalyst::View::Valiant::HTMLBuilder::$component"); | ||
| 35 | $module->new( | ||
| 36 | view=>$self, | ||
| 37 | context=>$self->ctx, | ||
| 38 | controller=>$self->ctx->controller, | ||
| 39 | ); | ||
| 40 | }; | ||
| 41 | }; | ||
| 42 | Moo::_Utils::_install_tracked(__PACKAGE__, $component_method_name, $sub); | ||
| 43 | } | ||
| 44 | |||
| 45 | my $_SELF; | ||
| 46 | sub BUILD { $_SELF = shift } | ||
| 47 | |||
| 48 | sub import { | ||
| 49 | my $class = shift; | ||
| 50 | my $target = caller; | ||
| 51 | my @args = @_; | ||
| 52 | |||
| 53 | $target->meta->superclasses($class); | ||
| 54 | $class->_install_helpers($target, @args); | ||
| 55 | $class->_install_tags($target); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _install_helpers { | ||
| 59 | my $class = shift; | ||
| 60 | my $target = shift; | ||
| 61 | my @args = @_; | ||
| 62 | |||
| 63 | foreach my $helper (@args) { | ||
| 64 | my $sub = Sub::Util::set_subname "${target}::${helper}" => sub { | ||
| 65 | my $self = shift; | ||
| 66 | croak "View method called without correct self" unless $self and $self->isa($target); | ||
| 67 | return $self->form->$helper(@_) if $self->form->can($helper); | ||
| 68 | return $self->pager->$helper(@_) if $self->pager->can($helper); | ||
| 69 | return $self->ctx->controller->$helper(@_) if $self->ctx->controller->can($helper); | ||
| 70 | return $self->ctx->$helper(@_) if $self->ctx->can($helper); | ||
| 71 | |||
| 72 | croak "Can't find helper '$helper' in form, pager, controller or context"; | ||
| 73 | }; | ||
| 74 | Moo::_Utils::_install_tracked($target, $helper, $sub); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | sub _install_tags { | ||
| 79 | my $class = shift; | ||
| 80 | my $target = shift; | ||
| 81 | my $tb = Module::Runtime::use_module('Valiant::HTML::Util::TagBuilder'); | ||
| 82 | |||
| 83 | my %tags = map { | ||
| 84 | ref $_ ? @$_ : ($_ => ucfirst($_) ); # up case the tag name | ||
| 85 | } (@Valiant::HTML::Util::TagBuilder::ALL_TAGS); | ||
| 86 | $tags{$_} = $_ for @_; | ||
| 87 | |||
| 88 | foreach my $tag (keys %tags) { | ||
| 89 | my $tag_name = $tags{$tag}; | ||
| 90 | |||
| 91 | my $method; | ||
| 92 | if(Valiant::HTML::Util::TagBuilder->is_content_tag($tag)) { | ||
| 93 | $method = Sub::Util::set_subname "${target}::${tag_name}" => sub { | ||
| 94 | my ($args, $content) = (+{}, ''); | ||
| 95 | $args = shift if ref $_[0] eq 'HASH'; | ||
| 96 | if(defined($_[0])) { | ||
| 97 | if(Scalar::Util::blessed($_[0]) && $_[0]->isa($class)) { | ||
| 98 | $content = shift->get_rendered; | ||
| 99 | } elsif((ref($_[0])||'') eq 'ARRAY') { | ||
| 100 | my $inner = shift; | ||
| 101 | my @content = map { | ||
| 102 | (Scalar::Util::blessed($_) && $_->isa($class)) ? $_->get_rendered : $_; | ||
| 103 | } @{$inner}; | ||
| 104 | $content = $class->safe_concat(@content); | ||
| 105 | } else { | ||
| 106 | $content = shift; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | return $_SELF->tb->tags->$tag($args, $content), @_ if @_; | ||
| 110 | return $_SELF->tb->tags->$tag($args, $content); | ||
| 111 | }; | ||
| 112 | } elsif(Valiant::HTML::Util::TagBuilder->is_void_tag($tag)) { | ||
| 113 | $method = Sub::Util::set_subname "${target}::${tag_name}" => sub { | ||
| 114 | my $args = +{}; | ||
| 115 | $args = shift if ref $_[0] eq 'HASH'; | ||
| 116 | return $_SELF->tb->tags->$tag($args), @_ if @_; | ||
| 117 | return $_SELF->tb->tags->$tag($args); | ||
| 118 | }; | ||
| 119 | } | ||
| 120 | Moo::_Utils::_install_tracked($target, $tag_name, $method); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | sub view { | ||
| 125 | my $self = shift; | ||
| 126 | my $view = shift; | ||
| 127 | my @args = (caller=>$self); | ||
| 128 | |||
| 129 | push @args, %{shift()} if ((ref($_[0])||'') eq 'HASH'); | ||
| 130 | push @args, shift if ((ref($_[0])||'') eq 'CODE'); | ||
| 131 | |||
| 132 | my $view_object = $self->ctx->view($view, @args); | ||
| 133 | |||
| 134 | return $view_object, @_ if @_; | ||
| 135 | return $view_object; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub read_attribute_for_html { | ||
| 139 | my ($self, $attribute) = @_; | ||
| 140 | return unless defined $attribute; | ||
| 141 | return my $value = $self->$attribute if $self->can($attribute); | ||
| 142 | die "No such attribute '$attribute' for view"; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub attribute_exists_for_html { | ||
| 146 | my ($self, $attribute) = @_; | ||
| 147 | return unless defined $attribute; | ||
| 148 | return 1 if $self->can($attribute); | ||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub flatten_rendered { | ||
| 153 | my ($self, @rendered) = @_; | ||
| 154 | return $self->safe_concat(@rendered); | ||
| 155 | } | ||
| 156 | |||
| 157 | my %data_templates = (); | ||
| 158 | sub data_template { | ||
| 159 | my $self = shift; | ||
| 160 | my $class = ref($self) || $self; | ||
| 161 | my $template = $data_templates{$class} ||= do { | ||
| 162 | my $data = "${class}::DATA"; | ||
| 163 | my $template = do { local $/; <$data> }; | ||
| 164 | }; | ||
| 165 | |||
| 166 | return $self->tb->sf($self, $template, {raw=>1}); | ||
| 167 | } | ||
| 168 | |||
| 169 | sub safe { shift; return Valiant::HTML::SafeString::safe(@_) } | ||
| 170 | sub raw {shift; return Valiant::HTML::SafeString::raw(@_) } | ||
| 171 | sub safe_concat { shift; return Valiant::HTML::SafeString::safe_concat(@_) } | ||
| 172 | sub escape_html { shift; return Valiant::HTML::SafeString::escape_html(@_) } | ||
| 173 | sub escape_javascript { shift; return Valiant::JSON::Util::escape_javascript(@_) } | ||
| 174 | sub escape_js { shift->escape_javascript(@_) } | ||
| 175 | |||
| 176 | sub uri_escape { | ||
| 177 | my $self = shift; | ||
| 178 | if(scalar(@_) > 1) { | ||
| 179 | my %pairs = @_; | ||
| 180 | return join '&', map { URI::Escape::uri_escape($_) . '=' . URI::Escape::uri_escape($pairs{$_}) } keys %pairs; | ||
| 181 | } else { | ||
| 182 | my $string = shift; | ||
| 183 | return URI::Escape::uri_escape($string); | ||
| 184 | |||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | around 'get_rendered' => sub { | ||
| 189 | my ($orig, $self, @args) = @_; | ||
| 190 | if($self->has_view_fragment) { | ||
| 191 | my $method = $self->view_fragment; | ||
| 192 | return $self->$method; | ||
| 193 | } else { | ||
| 194 | return $self->$orig(@args); | ||
| 195 | } | ||
| 196 | }; | ||
| 197 | |||
| 198 | __PACKAGE__->config(content_type=>'text/html'); | ||
| 199 | __PACKAGE__->meta->make_immutable; | ||
| 200 | |||
| 201 | =head1 NAME | ||
| 202 | |||
| 203 | Catalyst::View::Valiant::HTMLBuilder - Per Request, strongly typed Views in code | ||
| 204 | |||
| 205 | =head1 SYNOPSIS | ||
| 206 | |||
| 207 | package Example::View::HTML::Home; | ||
| 208 | |||
| 209 | use Moose; | ||
| 210 | use Catalyst::View::Valiant::HTMLBuilder | ||
| 211 | qw(form_for link_to uri); | ||
| 212 | |||
| 213 | has 'page_title' => (is=>'ro', required=>1); | ||
| 214 | |||
| 215 | sub the_time ($self) { P {class=>'timestamp'}, scalar localtime} | ||
| 216 | |||
| 217 | sub render($self, $c, $content) { | ||
| 218 | return Html +{ lang=>'en' }, [ | ||
| 219 | Head [ | ||
| 220 | Title $self->page_title, | ||
| 221 | Meta +{ charset=>"utf-8" }, | ||
| 222 | Meta +{ name=>"viewport", content=>"width=device-width, initial-scale=1, shrink-to-fit=no" }, | ||
| 223 | Link +{ rel=>"stylesheet", type=>'text/css', href=>"/static/core.css" }, | ||
| 224 | Link +{ rel=>"stylesheet", | ||
| 225 | href=>"https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css", | ||
| 226 | integrity=>'sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N', | ||
| 227 | crossorigin=>"anonymous" }, | ||
| 228 | ($self->caller->can('css') ? $self->caller->css : ''), | ||
| 229 | ], | ||
| 230 | Body [ | ||
| 231 | $content, | ||
| 232 | Div $self->the_time, | ||
| 233 | Script +{ src=>'https://code.jquery.com/jquery-3.7.0.min.js', | ||
| 234 | integrity=>'sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=', | ||
| 235 | crossorigin=>"anonymous" }, '', | ||
| 236 | Script +{ src=>'https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js', | ||
| 237 | integrity=>'sha384-Fy6S3B9q64WdZWQUiU+q4/2Lc9npb8tCaSX9FK7E8HnRr0Jz8D6OP9dO5Vg3Q9ct', | ||
| 238 | crossorigin=>"anonymous" }, '', | ||
| 239 | ], | ||
| 240 | ]; | ||
| 241 | } | ||
| 242 | |||
| 243 | 1; | ||
| 244 | |||
| 245 | =head1 DESCRIPTION | ||
| 246 | |||
| 247 | B<WARNINGS>: Experimental code that I might need to break back compatibility in order | ||
| 248 | to fix issues. I've rewritten this from scratch twice already so if I do so again | ||
| 249 | and it breaks all your code, you've been warned. | ||
| 250 | |||
| 251 | This is a L<Catalyst::View> subclass that provides a way to write views in code | ||
| 252 | that are strongly typed and per request. It also integrates with several of L<Valiant>'s | ||
| 253 | HTML form generation code modules to make it easier to create HTML forms that properly | ||
| 254 | synchronize with your L<Valiant> models for displaying errors and performing validation. | ||
| 255 | |||
| 256 | Unlike most Catalyst views, this view is 'per request' in that it is instantiated for | ||
| 257 | each request. This allows you to store per request state in the view object as well as | ||
| 258 | localize view specific logic to the view object. In particular it allows you to avoid or | ||
| 259 | reduce using the stash in order to pass values from the controller to the view. I think | ||
| 260 | this can make your views more robust and easier to support for the long term. It builds | ||
| 261 | upons L<Catalyst::View::BasePerRequest> which provides the per request behavior so you should | ||
| 262 | take a look at the documentation and example controller integration in that module in | ||
| 263 | order to get the idea. | ||
| 264 | |||
| 265 | As a quick example here's a possible controller that might invoke the view given in the | ||
| 266 | SYNOPSIS: | ||
| 267 | |||
| 268 | package Example::Controller::Home; | ||
| 269 | |||
| 270 | use Moose; | ||
| 271 | use MooseX::MethodAttributes; | ||
| 272 | |||
| 273 | extends 'Catalyst::Controller'; | ||
| 274 | |||
| 275 | sub index($self, $c) { | ||
| 276 | my $view = $c->view('HTML::Home', page_title=>'The Home Page'); | ||
| 277 | } | ||
| 278 | |||
| 279 | 1; | ||
| 280 | |||
| 281 | This will then work with the commonly used L<Catalyst::Action::RenderView> or my | ||
| 282 | L<Catalyst::ActionRole::RenderView> to produce a view response and set it as the | ||
| 283 | response body. | ||
| 284 | |||
| 285 | This approach is experimental and I expect best practices around it to evolve so don't | ||
| 286 | get stuck on any particular way of doing things. | ||
| 287 | |||
| 288 | =head1 ATTRIBUTES | ||
| 289 | |||
| 290 | This class inherits all of the attributes from L<Catalyst::View::BasePerRequest> | ||
| 291 | |||
| 292 | =head1 METHODS | ||
| 293 | |||
| 294 | This class inherits all of the methods from L<Catalyst::View::BasePerRequest> as well as: | ||
| 295 | |||
| 296 | =head2 tb | ||
| 297 | |||
| 298 | Instance of L<Valiant::HTML::Util::TagBuilder> that is used to generate HTML tags. | ||
| 299 | |||
| 300 | Returns the current C<form> object. | ||
| 301 | |||
| 302 | =head2 view | ||
| 303 | |||
| 304 | Given the name of a view, returns the view object. Basically a wrapper around C<< $c->view >>. | ||
| 305 | |||
| 306 | =head2 data_template | ||
| 307 | |||
| 308 | Returns the data template for the current view. This is a template that is used to generate | ||
| 309 | data attributes for the view. The template is read from a file named C<DATA> in the view | ||
| 310 | class. The template is processed by the C<sf> method in L<Valiant::HTML::TagBuilder> so | ||
| 311 | you can use the same syntax as you would in a view. Example | ||
| 312 | |||
| 313 | package Example::View::HTML::Home; | ||
| 314 | |||
| 315 | use Moose; | ||
| 316 | use Catalyst::View::Valiant::HTMLBuilder; | ||
| 317 | |||
| 318 | has bar => (is=>'ro', required=>1); | ||
| 319 | |||
| 320 | sub render($self, $c, $content) { | ||
| 321 | return Div +{ $self->data_template }, $content; | ||
| 322 | } | ||
| 323 | |||
| 324 | 1; | ||
| 325 | |||
| 326 | __DATA__ | ||
| 327 | data-foo="{:bar}" | ||
| 328 | |||
| 329 | =head2 safe | ||
| 330 | |||
| 331 | Marks a string as safe to render by first escaping it and then wrapping it in a L<Valiant::HTML::SafeString> object. | ||
| 332 | |||
| 333 | =head2 raw | ||
| 334 | |||
| 335 | Marks a string as safe to render by wrapping it in a L<Valiant::HTML::SafeString> object. | ||
| 336 | |||
| 337 | =head2 safe_concat | ||
| 338 | |||
| 339 | Given one or more strings and / or L<Valiant::HTML::SafeString> objects, returns | ||
| 340 | a new L<Valiant::HTML::SafeString> object that is the concatenation of all of the strings. | ||
| 341 | |||
| 342 | =head2 escape_html | ||
| 343 | |||
| 344 | Given a string, returns a new string that is the escaped version of the original string. | ||
| 345 | |||
| 346 | =head2 escape_javascript | ||
| 347 | |||
| 348 | Given a string, returns a new string that is the javascript escaped version of the original string. | ||
| 349 | |||
| 350 | =head2 uri_escape | ||
| 351 | |||
| 352 | Given a string, returns a new string that is the URI escaped version of the original string. | ||
| 353 | Given an array, returns a string that is the URI escaped version of the key value pairs in the array. | ||
| 354 | |||
| 355 | =head2 read_attribute_for_html | ||
| 356 | |||
| 357 | Given an attribute name, returns the value of that attribute if it exists. If the attribute does not exist, it will die. | ||
| 358 | |||
| 359 | =head2 attribute_exists_for_html | ||
| 360 | |||
| 361 | Given an attribute name, returns true if the attribute exists and false if it does notu. | ||
| 362 | |||
| 363 | =head2 formbuilder_class | ||
| 364 | |||
| 365 | sub formbuilder_class { 'Example::FormBuilder' } | ||
| 366 | |||
| 367 | Provides an easy way to override the default formbuilder class. By default it will use | ||
| 368 | L<Valiant::HTML::FormBuilder>. You can override this method to return a different class | ||
| 369 | via a subclass of this view. | ||
| 370 | |||
| 371 | =head1 EXPORTS | ||
| 372 | |||
| 373 | All HTML tags are exported as functions. For example: | ||
| 374 | |||
| 375 | package Example::View::HTML::Home; | ||
| 376 | |||
| 377 | use Moose; | ||
| 378 | use Catalyst::View::Valiant::HTMLBuilder; | ||
| 379 | |||
| 380 | sub render($self, $c, $content) { | ||
| 381 | return Div +{ class=>'foo' }, $content; | ||
| 382 | } | ||
| 383 | |||
| 384 | The export is the same as the HTML tag name but with the first letter capitalized. | ||
| 385 | |||
| 386 | In addition you can request to export any method from the form object, the pager object, | ||
| 387 | the controller object or the context object. For example: | ||
| 388 | |||
| 389 | package Example::View::HTML::Home; | ||
| 390 | |||
| 391 | use Moose; | ||
| 392 | use Catalyst::View::Valiant::HTMLBuilder | ||
| 393 | qw(form_for link_to uri); | ||
| 394 | |||
| 395 | sub render($self, $c, $content) { | ||
| 396 | return Div +{ class=>'foo' }, $content; | ||
| 397 | } | ||
| 398 | |||
| 399 | This will export the C<form_for>, C<link_to> and C<uri> methods from the form object, the | ||
| 400 | pager object, the controller object and the context object respectively. | ||
| 401 | |||
| 402 | See L<Valiant::HTML::Util::Form> and L<Valiant::HTML::Util::FormTags> for more information | ||
| 403 | on the form methods. | ||
| 404 | |||
| 405 | See L<Valiant::HTML::Util::Tagbuilder> for more information on the tag methods. | ||
| 406 | |||
| 407 | See L<Valiant::HTML::SafeString> for more information on the safe string methods. | ||
| 408 | |||
| 409 | See L<Valiant::JSON::Util> for more information on the JSON methods. | ||
| 410 | |||
| 411 | =head1 SUBCLASSING | ||
| 412 | |||
| 413 | Create a base class in your project: | ||
| 414 | |||
| 415 | package Example::View::HTML; | ||
| 416 | |||
| 417 | use Moose; | ||
| 418 | use Catalyst::View::Valiant::HTMLBuilder; | ||
| 419 | |||
| 420 | sub redirect_to_action ($self, $action, @args) { | ||
| 421 | return $self->redirect_to($self->uri_for_action($action, @args)); | ||
| 422 | } | ||
| 423 | |||
| 424 | sub the_time ($self) { | ||
| 425 | return P {class=>'timestamp'}, scalar localtime; | ||
| 426 | } | ||
| 427 | |||
| 428 | __PACKAGE__->meta->make_immutable; | ||
| 429 | |||
| 430 | Then you can use it as in this example. The subclass will automatically | ||
| 431 | inherit any local methods and attributes from the base class. | ||
| 432 | |||
| 433 | package Example::View::HTML::Page; | ||
| 434 | |||
| 435 | use Moose; | ||
| 436 | use Example::View::HTML; | ||
| 437 | |||
| 438 | has 'page_title' => (is=>'ro', required=>1); | ||
| 439 | |||
| 440 | sub render($self, $c, $content) { | ||
| 441 | return Html +{ lang=>'en' }, [ | ||
| 442 | Head [ | ||
| 443 | Title $self->page_title, | ||
| 444 | Meta +{ charset=>"utf-8" }, | ||
| 445 | Meta +{ name=>"viewport", content=>"width=device-width, initial-scale=1, shrink-to-fit=no" }, | ||
| 446 | Link +{ rel=>"stylesheet", type=>'text/css', href=>"/static/core.css" }, | ||
| 447 | Link +{ rel=>"stylesheet", | ||
| 448 | href=>"https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css", | ||
| 449 | integrity=>'sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N', | ||
| 450 | crossorigin=>"anonymous" }, | ||
| 451 | ], | ||
| 452 | Body [ | ||
| 453 | $content, | ||
| 454 | Div $self->the_time, | ||
| 455 | ], | ||
| 456 | ]; | ||
| 457 | } | ||
| 458 | |||
| 459 | __PACKAGE__->meta->make_immutable; | ||
| 460 | |||
| 461 | =head1 SEE ALSO | ||
| 462 | |||
| 463 | L<Valiant>, L<Valiant::HTML::Util::Form>, L<Valiant::HTML::Util::FormTags>, | ||
| 464 | L<Valiant::HTML::Util::Tagbuilder>, L<Valiant::HTML::SafeString>. | ||
| 465 | |||
| 466 | =head1 AUTHOR | ||
| 467 | |||
| 468 | See L<Valiant> | ||
| 469 | |||
| 470 | =head1 COPYRIGHT & LICENSE | ||
| 471 | |||
| 472 | See L<Valiant> | ||
| 473 | |||
| 474 | =cut |