File: Util.pm

package info (click to toggle)
libhtml-formhandler-perl 0.40057-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,320 kB
  • ctags: 685
  • sloc: perl: 8,849; makefile: 2
file content (102 lines) | stat: -rw-r--r-- 2,437 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package HTML::FormHandler::Render::Util;
# ABSTRACT: rendering utility
use Sub::Exporter;
Sub::Exporter::setup_exporter({ exports => [ 'process_attrs', 'cc_widget', 'ucc_widget' ] } );


# this is a function for processing various attribute flavors
sub process_attrs {
    my ($attrs) = @_;

    my @use_attrs;
    my $javascript = delete $attrs->{javascript} || '';
    for my $attr( sort keys %$attrs ) {
        my $value = '';
        if( defined $attrs->{$attr} ) {
            if( ref $attrs->{$attr} eq 'ARRAY' ) {
                # we don't want class="" if no classes specified
                next unless scalar @{$attrs->{$attr}};
                $value = join (' ', @{$attrs->{$attr}} );
            }
            else {
                $value = $attrs->{$attr};
            }
        }
        push @use_attrs, sprintf( '%s="%s"', $attr, $value );
    }
    my $output = join( ' ', @use_attrs );
    $output = " $output" if length $output;
    $output .= " $javascript" if $javascript;
    return $output;
}

sub cc_widget {
    my $widget = shift;
    return '' unless $widget;
    if($widget eq lc $widget) {
        $widget =~ s/^(\w{1})/\u$1/g;
        $widget =~ s/_(\w{1})/\u$1/g;
    }
    return $widget;
}

sub ucc_widget {
    my $widget = shift;
    if($widget ne lc $widget) {
        $widget =~ s/::/_/g;
        $widget = ucfirst($widget);
        my @parts = $widget =~ /([A-Z][a-z]*)/g;
        $widget = join('_', @parts);
        $widget = lc($widget);
    }
    return $widget;
}


1;

__END__

=pod

=encoding UTF-8

=head1 NAME

HTML::FormHandler::Render::Util - rendering utility

=head1 VERSION

version 0.40057

=head1 SYNOPSIS

The 'process_attrs' takes a hashref and creates an attribute string
for constructing HTML.

    my $attrs => {
        some_attr => 1,
        placeholder => 'Enter email...",
        class => ['help', 'special'],
    };
    my $string = process_attrs($attrs);

...will produce:

    ' some_attr="1" placeholder="Enter email..." class="help special"'

If an arrayref is empty, it will be skipped. For a hash key of 'javascript'
only the value will be appended (without '$key=""');

=head1 AUTHOR

FormHandler Contributors - see HTML::FormHandler

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Gerda Shank.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut