Newer Older
273 lines | 5.952kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojolicious::Plugin::DefaultHelpers;
2
use Mojo::Base 'Mojolicious::Plugin';
3

            
4
use Mojo::ByteStream;
5
use Mojo::Util 'dumper';
6

            
7
sub register {
8
  my ($self, $app) = @_;
9

            
10
  # Controller alias helpers
11
  for my $name (qw(app flash param stash session url_for validation)) {
12
    $app->helper($name => sub { shift->$name(@_) });
13
  }
14

            
15
  # Stash key shortcuts (should not generate log messages)
16
  for my $name (qw(extends layout title)) {
17
    $app->helper(
18
      $name => sub {
19
        my $self  = shift;
20
        my $stash = $self->stash;
21
        $stash->{$name} = shift if @_;
22
        $self->stash(@_) if @_;
23
        return $stash->{$name};
24
      }
25
    );
26
  }
27

            
28
  $app->helper(config => sub { shift->app->config(@_) });
29
  $app->helper(content       => \&_content);
30
  $app->helper(content_for   => \&_content_for);
31
  $app->helper(current_route => \&_current_route);
32
  $app->helper(dumper        => sub { shift; dumper(@_) });
33
  $app->helper(include       => \&_include);
34
  $app->helper(ua => sub { shift->app->ua });
35
  $app->helper(url_with => \&_url_with);
36
}
37

            
38
sub _content {
39
  my ($self, $name, $content) = @_;
40
  $name ||= 'content';
41

            
42
  # Set (first come)
43
  my $c = $self->stash->{'mojo.content'} ||= {};
44
  $c->{$name} //= ref $content eq 'CODE' ? $content->() : $content
45
    if defined $content;
46

            
47
  # Get
48
  return Mojo::ByteStream->new($c->{$name} // '');
49
}
50

            
51
sub _content_for {
52
  my ($self, $name, $content) = @_;
53
  return _content($self, $name) unless defined $content;
54
  my $c = $self->stash->{'mojo.content'} ||= {};
55
  return $c->{$name} .= ref $content eq 'CODE' ? $content->() : $content;
56
}
57

            
58
sub _current_route {
59
  return '' unless my $endpoint = shift->match->endpoint;
60
  return $endpoint->name unless @_;
61
  return $endpoint->name eq shift;
62
}
63

            
64
sub _include {
65
  my $self     = shift;
66
  my $template = @_ % 2 ? shift : undef;
67
  my $args     = {@_, defined $template ? (template => $template) : ()};
68

            
69
  # "layout" and "extends" can't be localized
70
  my $layout  = delete $args->{layout};
71
  my $extends = delete $args->{extends};
72

            
73
  # Localize arguments
74
  my @keys = keys %$args;
75
  local @{$self->stash}{@keys} = @{$args}{@keys};
76

            
77
  return $self->render(partial => 1, layout => $layout, extends => $extends);
78
}
79

            
80
sub _url_with {
81
  my $self = shift;
82
  return $self->url_for(@_)->query($self->req->url->query->clone);
83
}
84

            
85
1;
86

            
87
=encoding utf8
88

            
89
=head1 NAME
90

            
91
Mojolicious::Plugin::DefaultHelpers - Default helpers plugin
92

            
93
=head1 SYNOPSIS
94

            
95
  # Mojolicious
96
  $self->plugin('DefaultHelpers');
97

            
98
  # Mojolicious::Lite
99
  plugin 'DefaultHelpers';
100

            
101
=head1 DESCRIPTION
102

            
103
L<Mojolicious::Plugin::DefaultHelpers> is a collection of renderer helpers for
104
L<Mojolicious>.
105

            
106
This is a core plugin, that means it is always enabled and its code a good
107
example for learning to build new plugins, you're welcome to fork it.
108

            
109
=head1 HELPERS
110

            
111
L<Mojolicious::Plugin::DefaultHelpers> implements the following helpers.
112

            
113
=head2 app
114

            
115
  %= app->secret
116

            
117
Alias for L<Mojolicious::Controller/"app">.
118

            
119
=head2 config
120

            
121
  %= config 'something'
122

            
123
Alias for L<Mojo/"config">.
124

            
125
=head2 content
126

            
127
  %= content foo => begin
128
    test
129
  % end
130
  %= content bar => 'Hello World!'
131
  %= content 'foo'
132
  %= content 'bar'
133
  %= content
134

            
135
Store partial rendered content in named buffer and retrieve it, defaults to
136
retrieving the named buffer C<content>, which is commonly used for the
137
renderers C<layout> and C<extends> features. Note that new content will be
138
ignored if the named buffer is already in use.
139

            
140
=head2 content_for
141

            
142
  % content_for foo => begin
143
    test
144
  % end
145
  %= content_for 'foo'
146

            
147
Append partial rendered content to named buffer and retrieve it. Note that
148
named buffers are shared with the L</"content"> helper.
149

            
150
  % content_for message => begin
151
    Hello
152
  % end
153
  % content_for message => begin
154
    world!
155
  % end
156
  %= content_for 'message'
157

            
158
=head2 current_route
159

            
160
  % if (current_route 'login') {
161
    Welcome to Mojolicious!
162
  % }
163
  %= current_route
164

            
165
Check or get name of current route.
166

            
167
=head2 dumper
168

            
169
  %= dumper {some => 'data'}
170

            
171
Dump a Perl data structure with L<Mojo::Util/"dumper">.
172

            
173
=head2 extends
174

            
175
  % extends 'blue';
176
  % extends 'blue', title => 'Blue!';
177

            
178
Extend a template. All additional values get merged into the L</"stash">.
179

            
180
=head2 flash
181

            
182
  %= flash 'foo'
183

            
184
Alias for L<Mojolicious::Controller/"flash">.
185

            
186
=head2 include
187

            
188
  %= include 'menubar'
189
  %= include 'menubar', format => 'txt'
190

            
191
Include a partial template, all arguments get localized automatically and are
192
only available in the partial template.
193

            
194
=head2 layout
195

            
196
  % layout 'green';
197
  % layout 'green', title => 'Green!';
198

            
199
Render this template with a layout. All additional values get merged into the
200
L</"stash">.
201

            
202
=head2 param
203

            
204
  %= param 'foo'
205

            
206
Alias for L<Mojolicious::Controller/"param">.
207

            
208
=head2 session
209

            
210
  %= session 'foo'
211

            
212
Alias for L<Mojolicious::Controller/"session">.
213

            
214
=head2 stash
215

            
216
  %= stash 'foo'
217
  % stash foo => 'bar';
218

            
219
Alias for L<Mojolicious::Controller/"stash">.
220

            
221
  %= stash('name') // 'Somebody'
222

            
223
=head2 title
224

            
225
  % title 'Welcome!';
226
  % title 'Welcome!', foo => 'bar';
227
  %= title
228

            
229
Page title. All additional values get merged into the L</"stash">.
230

            
231
=head2 ua
232

            
233
  %= ua->get('mojolicio.us')->res->dom->at('title')->text
234

            
235
Alias for L<Mojo/"ua">.
236

            
237
=head2 url_for
238

            
239
  %= url_for 'named', controller => 'bar', action => 'baz'
240

            
241
Alias for L<Mojolicious::Controller/"url_for">.
242

            
243
=head2 url_with
244

            
245
  %= url_with 'named', controller => 'bar', action => 'baz'
246

            
247
Does the same as L</"url_for">, but inherits query parameters from the current
248
request.
249

            
250
  %= url_with->query([page => 2])
251

            
252
=head2 validation
253

            
254
  %= validation->param('foo')
255

            
256
Alias for L<Mojolicious::Controller/"validation">.
257

            
258
=head1 METHODS
259

            
260
L<Mojolicious::Plugin::DefaultHelpers> inherits all methods from
261
L<Mojolicious::Plugin> and implements the following new ones.
262

            
263
=head2 register
264

            
265
  $plugin->register(Mojolicious->new);
266

            
267
Register helpers in L<Mojolicious> application.
268

            
269
=head1 SEE ALSO
270

            
271
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
272

            
273
=cut