Skip to content

Commit

Permalink
merge content and content_for helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 3, 2015
1 parent 9b53988 commit e3ac5b1
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

6.01 2015-03-02
6.01 2015-03-03
- Relaxed request-line handling in Mojo::Message::Request.
- Fixed code name in version command and built-in templates.

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -259,13 +259,13 @@ sub _finish {
if (my $new = $self->transactor->upgrade($old)) {
weaken $self;
$new->on(resume => sub { $self->_write($id) });
$c->{cb}->($self, $c->{tx} = $new);
$c->{cb}($self, $c->{tx} = $new);
return $new->client_read($old->res->content->leftovers);
}

# Finish normal connection and handle redirects
$self->_remove($id, $close);
$c->{cb}->($self, $old) unless $self->_redirect($c, $old);
$c->{cb}($self, $old) unless $self->_redirect($c, $old);
}

sub _loop { $_[1] ? Mojo::IOLoop->singleton : $_[0]->ioloop }
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -117,7 +117,7 @@ sub handler {

# Dispatcher has to be last in the chain
++$self->{dispatch}
and $self->hook(around_action => sub { $_[2]->($_[1]) })
and $self->hook(around_action => sub { $_[2]($_[1]) })
and $self->hook(around_dispatch => sub { $_[1]->app->dispatch($_[1]) })
unless $self->{dispatch};

Expand Down
36 changes: 17 additions & 19 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -25,9 +25,12 @@ sub register {
$app->helper(c => sub { shift; Mojo::Collection->new(@_) });
$app->helper(config => sub { shift->app->config(@_) });

$app->helper(content => sub { _content(shift, 0, @_) });
$app->helper(content_for => sub { _content(shift, 1, @_) });

$app->helper($_ => $self->can("_$_"))
for qw(content content_for csrf_token current_route delay),
qw(inactivity_timeout is_fresh url_with);
for
qw(csrf_token current_route delay inactivity_timeout is_fresh url_with);

$app->helper(dumper => sub { shift; dumper @_ });
$app->helper(include => sub { shift->render_to_string(@_) });
Expand All @@ -45,26 +48,21 @@ sub _asset {
$c->rendered;
}

sub _block { ref $_[0] eq 'CODE' ? $_[0]() : $_[0] }

sub _content {
my ($c, $name, $content) = @_;
my ($c, $append, $name, $content) = @_;
$name ||= 'content';

# Set (first come)
my $hash = $c->stash->{'mojo.content'} ||= {};
$hash->{$name} //= ref $content eq 'CODE' ? $content->() : $content
if defined $content;
if (defined $content) {
if ($append) { $hash->{$name} .= _block($content) }
else { $hash->{$name} //= _block($content) }
}

# Get
return Mojo::ByteStream->new($hash->{$name} // '');
}

sub _content_for {
my ($c, $name, $content) = @_;
return _content($c, $name) unless defined $content;
my $hash = $c->stash->{'mojo.content'} ||= {};
return $hash->{$name} .= ref $content eq 'CODE' ? $content->() : $content;
}

sub _csrf_token {
my $c = shift;
$c->session->{csrf_token}
Expand Down Expand Up @@ -238,9 +236,9 @@ Alias for L<Mojo/"config">.
%= content 'bar'
%= content
Store partial rendered content in named buffer and retrieve it, defaults to
retrieving the named buffer C<content>, which is commonly used for the
renderers C<layout> and C<extends> features. Note that new content will be
Store partial rendered content in a named buffer and retrieve it later,
defaults to retrieving the named buffer C<content>, which is commonly used for
the renderers C<layout> and C<extends> features. Note that new content will be
ignored if the named buffer is already in use.
=head2 content_for
Expand All @@ -250,8 +248,8 @@ ignored if the named buffer is already in use.
% end
%= content_for 'foo'
Append partial rendered content to named buffer and retrieve it. Note that
named buffers are shared with the L</"content"> helper.
Same as the L</"content"> helper, but appends more and more content to a named
buffer.
% content_for message => begin
Hello
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/EPRenderer.pm
Expand Up @@ -47,7 +47,7 @@ sub register {
local *{"${ns}::_C"} = sub {$c};

# Render with "epl" handler
return $renderer->handlers->{epl}->($renderer, $c, $output, $options);
return $renderer->handlers->{epl}($renderer, $c, $output, $options);
}
);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -112,20 +112,20 @@ sub render {
# Data
my $output;
if (defined(my $data = delete $stash->{data})) {
$self->handlers->{data}->($self, $c, \$output, {data => $data});
$self->handlers->{data}($self, $c, \$output, {data => $data});
return $output, $options->{format};
}

# JSON
elsif (exists $stash->{json}) {
my $json = delete $stash->{json};
$self->handlers->{json}->($self, $c, \$output, {json => $json});
$self->handlers->{json}($self, $c, \$output, {json => $json});
return $output, 'json';
}

# Text
elsif (defined(my $text = delete $stash->{text})) {
$self->handlers->{text}->($self, $c, \$output, {text => $text});
$self->handlers->{text}($self, $c, \$output, {text => $text});
}

# Template or templateless handler
Expand Down
2 changes: 1 addition & 1 deletion lib/ojo.pm
Expand Up @@ -17,7 +17,7 @@ sub import {
my $caller = caller;
eval "package $caller; use Mojolicious::Lite; 1" or die $@;
my $ua = $caller->app->ua;
$ua->server->app->hook(around_action => sub { local $_ = $_[1]; $_[0]->() });
$ua->server->app->hook(around_action => sub { local $_ = $_[1]; $_[0]() });

$ua->max_redirects(10) unless defined $ENV{MOJO_MAX_REDIRECTS};
$ua->proxy->detect unless defined $ENV{MOJO_PROXY};
Expand Down

0 comments on commit e3ac5b1

Please sign in to comment.