Skip to content

Commit

Permalink
simplify content and content_for helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 17, 2012
1 parent 6cbcefa commit 300d8a5
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -82,31 +82,23 @@ sub register {
}

sub _content {
my $self = shift;
my $name = shift || 'content';
my $content = pop;
my ($self, $name, $content) = @_;
$name ||= 'content';

# Set
# Set (first come)
my $c = $self->stash->{'mojo.content'} ||= {};
if (defined $content) {

# Reset with multiple values
if (@_) {
$c->{$name}
= join('', map({ref $_ eq 'CODE' ? $_->() : $_} @_, $content));
}

# First come
else { $c->{$name} ||= ref $content eq 'CODE' ? $content->() : $content }
}
$c->{$name} ||= ref $content eq 'CODE' ? $content->() : $content
if defined $content;

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

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

sub _current_route {
Expand Down

0 comments on commit 300d8a5

Please sign in to comment.