| 1 | package Yukki::TemplateUtil; | ||
| 2 | $Yukki::TemplateUtil::VERSION = '0.991_002'; # TRIAL | ||
| 3 | |||
| 4 | $Yukki::TemplateUtil::VERSION = '0.991002';use v5.24; | ||
| 5 | use utf8; | ||
| 6 | |||
| 7 | use Mojo::DOM58::Entities qw( html_escape ); | ||
| 8 | use Try::Tiny; | ||
| 9 | |||
| 10 | use namespace::clean; | ||
| 11 | |||
| 12 | # ABSTRACT: Utiltiies that help make manipulating the DOM easier | ||
| 13 | |||
| 14 | |||
| 15 | use Sub::Exporter -setup => { | ||
| 16 | exports => [ qw( | ||
| 17 | field | ||
| 18 | form_error | ||
| 19 | ) ], | ||
| 20 | }; | ||
| 21 | |||
| 22 | |||
| 23 | sub field { | ||
| 24 | my ($paths, $default) = @_; | ||
| 25 | $paths = [$paths] unless ref $paths; | ||
| 26 | |||
| 27 | sub { | ||
| 28 | my ($template, $dom, $data) = @_; | ||
| 29 | |||
| 30 | my $value; | ||
| 31 | for my $path (@$paths) { | ||
| 32 | $value = try { | ||
| 33 | $template->data_at_path($data, $path); | ||
| 34 | }; | ||
| 35 | return $value if defined $value; | ||
| 36 | } | ||
| 37 | |||
| 38 | return ''; | ||
| 39 | }; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | sub form_error { | ||
| 44 | my ($data_path) = @_; | ||
| 45 | my $path = "form_errors.$data_path"; | ||
| 46 | |||
| 47 | sub { | ||
| 48 | my ($template, $dom, $data) = @_; | ||
| 49 | |||
| 50 | my $form_error = try { | ||
| 51 | $template->data_at_path($data, $path); | ||
| 52 | }; | ||
| 53 | return '' unless defined $form_error; | ||
| 54 | |||
| 55 | my $error = join ' ', @$form_error; | ||
| 56 | |||
| 57 | $dom->append( | ||
| 58 | qq[<div class="field-message error">] | ||
| 59 | . html_escape($error) | ||
| 60 | . qq[</div>] | ||
| 61 | ); | ||
| 62 | return ''; | ||
| 63 | }; | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; | ||
| 67 | |||
| 68 | __END__ | ||
| 69 | |||
| 70 | =pod | ||
| 71 | |||
| 72 | =encoding UTF-8 | ||
| 73 | |||
| 74 | =head1 NAME | ||
| 75 | |||
| 76 | Yukki::TemplateUtil - Utiltiies that help make manipulating the DOM easier | ||
| 77 | |||
| 78 | =head1 VERSION | ||
| 79 | |||
| 80 | version 0.991_002 | ||
| 81 | |||
| 82 | =head1 DESCRIPTION | ||
| 83 | |||
| 84 | Tools for manipulating the DOM in specialized wasy inside of L<Template::Pure> templates. | ||
| 85 | |||
| 86 | =head1 SUBROUTINES | ||
| 87 | |||
| 88 | =head2 field | ||
| 89 | |||
| 90 | $view->prepare_template( | ||
| 91 | template => 'admin/user/edit.html', | ||
| 92 | directives => [ | ||
| 93 | ... | ||
| 94 | '#email@value' => field(['user.email', 'form.email']), | ||
| 95 | ... | ||
| 96 | ], | ||
| 97 | ); | ||
| 98 | |||
| 99 | L<Template::Pure> is touchy about missing paths. This will make sure a field is | ||
| 100 | present so the template renders okay without requiring any additional | ||
| 101 | boilerplate. First argument is the name of the data path or paths to lookup. | ||
| 102 | Multiple paths may be passed using an array reference. The second value | ||
| 103 | (optional) is the default to use if that finds nothing. If no default is given, | ||
| 104 | the default default is an empty string. | ||
| 105 | |||
| 106 | =head2 form_error | ||
| 107 | |||
| 108 | $view->prepare_template( | ||
| 109 | template => 'admin/user/edit.html', | ||
| 110 | directives => [ | ||
| 111 | ... | ||
| 112 | '#email' => form_error('email'), | ||
| 113 | ... | ||
| 114 | ], | ||
| 115 | ); | ||
| 116 | |||
| 117 | Appends content after an element to insert code to show field errors, if field | ||
| 118 | errors are set. | ||
| 119 | |||
| 120 | =head1 AUTHOR | ||
| 121 | |||
| 122 | Andrew Sterling Hanenkamp <hanenkamp@cpan.org> | ||
| 123 | |||
| 124 | =head1 COPYRIGHT AND LICENSE | ||
| 125 | |||
| 126 | This software is copyright (c) 2017 by Qubling Software LLC. | ||
| 127 | |||
| 128 | This is free software; you can redistribute it and/or modify it under | ||
| 129 | the same terms as the Perl 5 programming language system itself. | ||
| 130 | |||
| 131 | =cut |