Created
October 20, 2010 14:56
-
-
Save sharifulin/636577 to your computer and use it in GitHub Desktop.
Include and process for Mojolicious app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use lib 'lib'; | |
| package Mojolicious::Plugin::IncludeHelpers; | |
| use strict; | |
| use warnings; | |
| use base 'Mojolicious::Plugin'; | |
| use Data::Dumper; | |
| sub register { | |
| my ($self, $app) = @_; | |
| $app->helper(process => sub { shift->render_partial(@_) }); | |
| $app->helper(include => sub { | |
| my $self = shift; | |
| my $data = $self->render_partial(@_); | |
| # warn Dumper $self->stash; | |
| my $captures = $self->{stash}->{'mojo.captures'}; | |
| $self->stash($_ => $captures->{$_}) for grep { $self->stash($_) } keys %{$captures||{}}; | |
| # warn Dumper $self->stash; | |
| return $data; | |
| }); | |
| } | |
| package main; | |
| use Mojolicious::Lite; | |
| plugin 'include_helpers'; | |
| get '/:template' => { user => 'Foo' }; | |
| app->log->level('error'); | |
| use Test::Mojo; | |
| use Test::More tests => 6; | |
| my $t = Test::Mojo->new; | |
| $t->get_ok('/process') | |
| ->status_is(200) | |
| ->content_is("Foo\nHello, Bar!\n\nBar\n") | |
| ; | |
| $t->get_ok('/include') | |
| ->status_is(200) | |
| ->content_is("Foo\nHello, Bar!\n\nFoo\n") | |
| ; | |
| __DATA__ | |
| @@ process.html.ep | |
| %= stash 'user' | |
| %= process 'inc', user => 'Bar' | |
| %= stash 'user' | |
| @@ include.html.ep | |
| %= stash 'user' | |
| %= include 'inc', user => 'Bar' | |
| %= stash 'user' | |
| @@ inc.html.ep | |
| Hello, <%= $user %>! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment