Skip to content
This repository has been archived by the owner on Feb 20, 2018. It is now read-only.

Commit

Permalink
very stupid template rendering. needs massive refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Mar 23, 2010
1 parent 1275fd9 commit 0579143
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
80 changes: 80 additions & 0 deletions lib/Prong/Server/Gadget.pm
@@ -0,0 +1,80 @@
package Prong::Server::Gadget;
use Moose;
use Prong::Schema;
use Plack::Request;
use Router::Simple;
use Text::MicroTemplate::File;
use namespace::autoclean;

has router => (
is => 'ro',
isa => 'Router::Simple',
lazy_build => 1,
);

has template => (
is => 'ro',
isa => 'Text::MicroTemplate::File',
required => 1,
);

has schema => (
is => 'ro',
isa => 'Prong::Schema',
required => 1,
);

sub _build_router {
my $self = shift;

my $router = Router::Simple->new();
$router->connect('/' => {
code => sub {
my $req = shift;
my @modules = $self->schema->resultset('Module')->search();
return [
200,
[ "Content-Type" => "text/html" ],
[ $self->template->render_file( 'index.mt', $req, { modules => \@modules } ) ]
];
}
} );

$router->connect('/app/{module_id}' => {
code => sub {
my ($req, $p) = @_;
my $content = $self->schema->resultset('ModuleContent')->search({
module_id => $p->{module_id},
})->single;
return [
200,
[ "Content-Type" => "text/html" ],
[ $self->template->render_file( 'app/view.mt', $req, { content => $content } ) ]
];
}
});

return $router;
}

sub process {
my ($self, $env) = @_;

if (my $p = $self->router->match( $env )) {
return $p->{code}->( Plack::Request->new( $env ), $p );
}

return [ 404, [], [ 'Not Found' ] ];
}

sub psgi_app {
my $self = shift;
return sub {
my $env = shift;
$self->process( $env );
};
}

__PACKAGE__->meta->make_immutable();

1;
16 changes: 16 additions & 0 deletions misc/app.psgi
@@ -0,0 +1,16 @@
use lib "lib";
use Prong::Server::Gadget;

Prong::Server::Gadget->new(
schema => Prong::Schema->connect(
'dbi:mysql:dbname=prong',
'root',
undef
),
template => Text::MicroTemplate::File->new(
use_cache => 1,
include_path => [
'templates'
]
)
)->psgi_app;
6 changes: 6 additions & 0 deletions templates/app/view.mt
@@ -0,0 +1,6 @@
? my ($req, $stash) = @_;
? $_mt->wrapper_file('wrapper.mt')->(sub {

<?= Text::MicroTemplate::encoded_string($stash->{content}->content) ?>

? });
8 changes: 8 additions & 0 deletions templates/index.mt
@@ -0,0 +1,8 @@
? my ($req, $stash) = @_;
? $_mt->wrapper_file('wrapper.mt')->(sub {

? foreach my $module (@{ $stash->{modules} }) {
<a href="/app/<?= $module->id ?>"><?= $module->title ?></a>
? }

? });
8 changes: 8 additions & 0 deletions templates/wrapper.mt
@@ -0,0 +1,8 @@
<html>
<head>
<title>Test</title>
</head>
<body>
<?= $_[0] ?>
</body>
</html>

0 comments on commit 0579143

Please sign in to comment.