File: TagHelpers.pm

package info (click to toggle)
libmojolicious-perl 0.999926-1%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,672 kB
  • ctags: 949
  • sloc: perl: 17,391; makefile: 4
file content (245 lines) | stat: -rw-r--r-- 5,397 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Copyright (C) 2008-2010, Sebastian Riedel.

package Mojolicious::Plugin::TagHelpers;

use strict;
use warnings;

use base 'Mojolicious::Plugin';

use Mojo::ByteStream;

# Is today's hectic lifestyle making you tense and impatient?
# Shut up and get to the point!
sub register {
    my ($self, $app) = @_;

    # Add "form_for" helper
    $app->renderer->add_helper(
        form_for => sub {
            my $c    = shift;
            my $name = shift;

            # Captures
            my $captures = ref $_[0] eq 'HASH' ? shift : {};

            $self->_tag('form', action => $c->url_for($name, $captures), @_);
        }
    );

    # Add "img" helper
    $app->renderer->add_helper(
        img => sub { shift; $self->_tag('img', src => shift, @_) });

    # Add "input" helper
    $app->renderer->add_helper(
        input => sub {
            my $c    = shift;
            my $name = shift;

            # Value
            if (defined(my $p = $c->param($name))) {

                # Attributes
                my %attrs = @_;

                # Checkbox
                if (($attrs{type} || '') eq 'checkbox') {
                    $attrs{checked} = 'checked';
                }

                # Other
                else { $attrs{value} = $p }

                return $self->_tag('input', name => $name, %attrs);
            }

            # Empty tag
            $self->_tag('input', name => $name, @_);
        }
    );

    # Add "label" helper
    $app->renderer->add_helper(
        label => sub { shift; $self->_tag('label', for => shift, @_) });

    # Add "link_to" helper
    $app->renderer->add_helper(
        link_to => sub {
            my $c    = shift;
            my $name = shift;

            # Captures
            my $captures = ref $_[0] eq 'HASH' ? shift : {};

            # Default content
            push @_, sub { $name = Mojo::ByteStream->new($name)->xml_escape->to_string; ucfirst $name }
              unless defined $_[-1] && ref $_[-1] eq 'CODE';

            $self->_tag('a', href => $c->url_for($name, $captures), @_);
        }
    );

    # Add "script" helper
    $app->renderer->add_helper(
        script => sub {
            my $c = shift;

            # Path
            if (@_ % 2 ? ref $_[-1] ne 'CODE' : ref $_[-1] eq 'CODE') {
                return $self->_tag(
                    'script',
                    src  => shift,
                    type => 'text/javascript',
                    @_
                );
            }

            # Block
            $self->_tag('script', type => 'text/javascript', @_);
        }
    );

    # Add "tag" helper
    $app->renderer->add_helper(tag => sub { shift; $self->_tag(@_) });
}

sub _tag {
    my $self = shift;
    my $name = shift;

    # Callback
    my $cb = defined $_[-1] && ref($_[-1]) eq 'CODE' ? pop @_ : undef;
    pop if @_ % 2;

    # Tag
    my $tag = "<$name";

    # Attributes
    my %attrs = @_;
    for my $key (sort keys %attrs) {
        my $value = $attrs{$key};
        $value = Mojo::ByteStream->new($value)->xml_escape->to_string;
        $tag .= qq/ $key="$value"/;
    }

    # Block
    if ($cb) {
        $tag .= '>';
        $tag .= $cb->();
        $tag .= "<\/$name>";
    }

    # Empty element
    else { $tag .= ' />' }

    # Prevent escaping
    return Mojo::ByteStream->new($tag);
}

1;
__END__

=head1 NAME

Mojolicious::Plugin::TagHelpers - Tag Helpers Plugin

=head1 SYNOPSIS

    # Mojolicious
    $self->plugin('tag_helpers');

    # Mojolicious::Lite
    plugin 'tag_helpers';

=head1 DESCRIPTION

L<Mojolicous::Plugin::TagHelpers> is a collection of HTML5 tag helpers for
L<Mojolicious>.
Note that this module is EXPERIMENTAL and might change without warning!

=head2 HELPERS

=over 4

=item form_for

    <%= form_for login => (method => 'post') => {%>
        <%= input 'first_name' %>
    <%}%>
    <%= form_for login => {foo => 'bar'} => (method => 'post') => {%>
        <%= input 'first_name' %>
    <%}%>
    <%= form_for '/login' => (method => 'post') => {%>
        <%= input 'first_name' %>
    <%}%>
    <%= form_for 'http://mojolicious.org/login' => (method => 'post') => {%>
        <%= input 'first_name' %>
    <%}%>

Generate form for route, path or URL.

=item img

    <%= img '/foo.jpg' %>
    <%= img '/foo.jpg', alt => 'Image' %>

Generate image tag.

=item input

    <%= input 'first_name' %>
    <%= input 'first_name', value => 'Default name' %>

Generate form input element.

=item label

    <%= label first_name => {%>First name<%}%>

Generate form label.

=item link_to

    <%= link_to index => {%>Home<%}%>
    <%= link_to index => {foo => 'bar'} => (class => 'links') => {%>Home<%}%>
    <%= link_to '/path/to/file' => {%>File<%}%>
    <%= link_to 'http://mojolicious.org' => {%>Mojolicious<%}%>

Generate link to route, path or URL.

=item script

    <%= script '/script.js' %>
    <%= script {%>
        var a = 'b';
    <%}%>

Generate script tag.

=item tag

    <%= tag 'div' %>
    <%= tag 'div', id => 'foo' %>
    <%= tag div => {%>Content<%}%>

HTML5 tag generator.

=back

=head1 METHODS

L<Mojolicious::Plugin::TagHelpers> inherits all methods from
L<Mojolicious::Plugin> and implements the following new ones.

=head2 C<register>

    $plugin->register;

Register helpers in L<Mojolicious> application.

=head1 SEE ALSO

L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.

=cut