Files
compiled.world/lib/CW/Plack/Response.pm
2026-08-15 23:05:57 -06:00

64 lines
1.4 KiB
Perl

package CW::Plack::Response;
use Moo;
use Carp ();
has _response => (
is => 'ro',
init_arg => 'response',
required => 1,
handles => [qw(finalize status body headers header)]
);
has request => (
is => 'ro',
required => 1,
handles => [qw(env)]
);
has h => (
is => 'ro',
required => 1
);
sub template {
my ( $self, $template, $ctx ) = @_;
my $html;
# If rendering inline
if ( ref($template) && ref($template) eq 'ARRAY' ) {
$html = $self->h->html($template);
}
elsif ( !ref($template) ) {
$ctx //= {};
$ctx->{env} = $self->env;
## no strict [BuiltinFunctions::ProhibitStringyEval]
my $template_pkg = "CW::Template::$template";
eval "use $template_pkg";
my $layout_pkg =
"CW::Layout::" . ( delete( $ctx->{layout} ) // 'Default' );
## no strict [BuiltinFunctions::ProhibitStringyEval]
eval "use $layout_pkg";
my $layout = $layout_pkg->new(
template => $template_pkg->new( ctx => $ctx ),
ctx => $ctx
);
$html = $self->h->html( $layout->render );
}
else {
Carp::croak("Invalid value passed to template call: $template");
}
$self->_response->header( 'Content-Type' => 'text/html; encoding=utf-8' );
$self->_response->body($html);
return $self;
}
1;