Skip to content

Commit

Permalink
add content_with helper to Mojolicious::Plugin::DefaultHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 3, 2015
1 parent 5fcc098 commit 275f0e7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

6.01 2015-03-03
- Added content_with helper to Mojolicious::Plugin::DefaultHelpers.
- Relaxed request-line handling in Mojo::Message::Request.
- Fixed code name in version command and built-in templates.

Expand Down
33 changes: 27 additions & 6 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -25,8 +25,9 @@ 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(content => sub { _content(shift, 0, 0, @_) });
$app->helper(content_for => sub { _content(shift, 1, 0, @_) });
$app->helper(content_with => sub { _content(shift, 0, 1, @_) });

$app->helper($_ => $self->can("_$_"))
for
Expand All @@ -39,7 +40,8 @@ sub register {

$app->helper('reply.exception' => sub { _development('exception', @_) });
$app->helper('reply.not_found' => sub { _development('not_found', @_) });
$app->helper(ua => sub { shift->app->ua });

$app->helper(ua => sub { shift->app->ua });
}

sub _asset {
Expand All @@ -51,13 +53,14 @@ sub _asset {
sub _block { ref $_[0] eq 'CODE' ? $_[0]() : $_[0] }

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

my $hash = $c->stash->{'mojo.content'} ||= {};
if (defined $content) {
if ($append) { $hash->{$name} .= _block($content) }
else { $hash->{$name} //= _block($content) }
if ($replace) { $hash->{$name} = _block($content) }
else { $hash->{$name} //= _block($content) }
}

return Mojo::ByteStream->new($hash->{$name} // '');
Expand Down Expand Up @@ -257,7 +260,25 @@ are already in use.
% content_for message => begin
world!
% end
%= content_for 'message'
%= content 'message'
=head2 content_with
% content_with foo => begin
test
% end
%= content_with 'foo'
Same as the L</"content"> helper, but replace content of named buffers if they
are already in use.
% content message => begin
world!
% end
% content_with message => begin
Hello <%= content 'message' %>
% end
%= content 'message'
=head2 csrf_token
Expand Down
15 changes: 15 additions & 0 deletions t/mojolicious/layouted_lite_app.t
Expand Up @@ -97,6 +97,8 @@ get '/withblocklayout' => sub {

get '/content_for';

get '/content_with';

get '/inline' => {inline => '<%= "inline!" %>'};

get '/inline/again' => {inline => 0};
Expand Down Expand Up @@ -235,6 +237,9 @@ $t->get_ok('/withblocklayout')->status_is(200)
$t->get_ok('/content_for')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->content_is("DefaultThis\n\nseems\nto\nHello world!\n\nwork!\n\n");
$t->get_ok('/content_with')->status_is(200)
->header_is(Server => 'Mojolicious (Perl)')
->content_is("Default\n\nSomething else!\n\n\nHello world!\n\n");

# Inline template
$t->get_ok('/inline')->status_is(200)
Expand Down Expand Up @@ -399,6 +404,16 @@ to
<%= content_for 'message' %>
work!
@@ content_with.html.ep
<% content first => begin %>Something<% end %>
<% content_for first => begin %> else!<% end %>
%= content_with 'first'
% content_with first => '';
%= content_with 'first'
<% content second => begin %>world<% end %>
<%= content_with second => begin %>Hello <%= content 'second' %>!<% end %>
% content_with 'second'
@@ layouts/variants.txt.ep
Variant: <%= content %>\
Expand Down

0 comments on commit 275f0e7

Please sign in to comment.