Kelp
view release on metacpan or search on metacpan
lib/Kelp/Manual.pod view on Meta::CPAN
Your routes don't always have to set the C<response> object. You could just
return a simple scalar value or a reference to a hash, array or anything that
can be converted to JSON.
# Content-type automatically set to "text/html"
sub text_route {
return "There, there ...";
}
# Content-type automatically set to "application/json"
sub json_route {
return { error => 1, message => "Fail" };
}
=head3 Automatic charset encoding
With Kelp, you don't have to worry about the encoding of the response - most of
the methods will automatically encode the response into configured
application's charset. Text and application content types will by default have
C<charset> part added. To make it all work flawlessly, remember to C<use utf8;>
at the top of your files.
If you'd like to instead take charset into your own hands, you can configure
L<Kelp/charset> and L<Kelp/request_charset> to undefined values. Alternatively,
you can use C<raw_> methods in L<Kelp::Request> and
L<Kelp::Response/render_binary> and manually set content types and charsets.
=head3 Rendering text
# Render simple text
$self->res->text->render("It works!");
=head3 Rendering HTML
$self->res->html->render("<h1>It works!</h1>");
=head3 Custom content type
$self->res->set_content_type('image/png');
=head3 Return 404 or 500 errors
sub some_route {
my $self = shift;
if ($missing) {
return $self->res->render_404;
}
if ($broken) {
return $self->res->render_500;
}
}
=head3 Templates
sub hello {
my ( $self, $name ) = @_;
$self->res->template( 'hello.tt', { name => $name } );
}
The above example will render the contents of C<hello.tt>, and it will set the
content-type to C<text/html>. To set a different content-type, use
C<set_content_type> or any of its aliases:
sub hello_txt {
my ( $self, $name ) = @_;
$self->res->text->template( 'hello_txt.tt', { name => $name } );
}
You can also simply get template string and return it, which will work the same:
sub hello {
my ( $self, $name ) = @_;
# NOTE: it's template method from $self, not from $self->res
return $self->template( 'hello.tt', { name => $name } );
}
=head3 Rendering DATA
Kelp templates can easily render from C<DATA> or other filehandle:
sub hello {
my ( $self, $name ) = @_;
return $self->template( \*DATA, { name => $name } );
}
__DATA__
Hello, [% name %]!
=head3 Headers
$self->set_header( "X-Framework", "Kelp" )->render( { success => \1 } );
=head3 Serving static files
If you want to serve static pages, you can use the L<Plack::Middleware::Static>
middleware that comes with Plack. Here is an example configuration that serves
files in your C<public> folder (under the Kelp root folder) from URLs that
begin with C</public>:
# conf/config.pl
{
middleware => [qw/Static/],
middleware_init => {
Static => {
path => qr{^/public/},
root => '.',
}
}
};
=head3 Delayed responses
To send a delayed response, have your route return a subroutine.
sub delayed {
my $self = shift;
return sub {
my $responder = shift;
$self->res->code(200);
$self->res->text->body("Better late than never.");
( run in 0.976 second using v1.01-cache-2.11-cpan-d7f47b0818f )