Skip to content

Commit

Permalink
Major update, in prep for merger with Bailador.
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Totten committed Mar 20, 2013
1 parent 0773b0a commit 6349658
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
blib
Makefile
*.pir
*.pbc
60 changes: 50 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,58 @@
An extension to [Web::App](https://github.com/supernovus/perl6-web/) which
adds an interface similar to Dancer or Bailador.

It currently supports SCGI and HTTP::Easy as transports, and Template6
and Flower::TAL as template engines. It will gain more as I develop it further.
NOTE: I am reworking this module to be merged with Bailador.
A lot of Bailador is duplicating the work done in the Web project, and
so I have agreed to work with tadzik to merge these two projects together.
I am currently adding any of Bailador's features that are missing, and will
then work with tadzik to make any transition required to merge the projects.

It's very much experimental, and not nearly as feature complete as I'd like.
## Supported Transports

It will assist me in expanding Web::App itself, as I figure out what features
are required to make this really useful.
Technically, it can use any backend transport that Web::App supports,
but there are only convenience wrappers provided for the following:

* [SCGI](https://github.com/supernovus/SCGI/)

The fastest way to connect to a web server such as Apache or lighttpd.
Type 'use-scgi' in your application to use this.

* [HTTP::Easy](https://github.com/supernovus/perl6-http-easy/)

A standalone HTTP server, useful for testing your in-development apps.
Type 'use-http' in your application to use this.
This is the default choice if you don't specify another option.

## Supported Template Engines

* [Template6](https://github.com/supernovus/template6/)

An engine inspired by Template Toolkit. Has many features.
Type 'use-template6' in your application to use this engine.
This is the default choice if you don't specify another option.

* [Flower::TAL](https://github.com/supernovus/flower/)

An implementation of the TAL/METAL XML-based template languages from Zope.
Type 'use-tal' in your application to use this engine.

* [Template::Mojo](https://github.com/tadzik/Template-Mojo/)

A template engine inspired by Perl 5's Mojo::Template.
Type 'use-mojo' in your application to use this engine.

* [HTML::Template](https://github.com/masak/html-template/)

A template engine inspired by Perl 5's HTML::Template.
Type 'use-html' in your application to use this engine.

## Example Application Script

```perl
use Web::App::Ballet;

use-template6; ## We're explicitly setting the template engine.

get '/' => sub ($c) {
$c.content-type: 'text/plain';
my $name = $c.get(:default<World>, 'name');
Expand All @@ -30,19 +69,20 @@ are required to make this really useful.
template 'about.tt', { :ver<1.0.0> }; ## Implicit output.
}

dance;
dance; ## Start the process.

```

## TODO

* Add more Transport handlers.
* Add more Template engines.
* Make it more useful (i.e. make the context object optional.)
* Add examples for the various template engines.
* Add testing ability once Web::App has testing support added.
* Add pathname placeholder support: get '/users/:username'
* Add support for the Plosurin template engine.

## Author

Timothy Totten, supernovus on #perl6, https://github.com/supernovus/
[Timothy Totten](https://github.com/supernovus/) -- supernovus on #perl6

## License

Expand Down
54 changes: 31 additions & 23 deletions lib/Web/App/Ballet.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,39 @@ sub use-http (Int $port = 8080) is export
$app-transport = ::('HTTP::Easy::PSGI').new(:port($port));
}

sub use-template6 (Str $path = './templates') is export
sub set-template-engine (Str $name, Str $path = './views') is export
{
require Template6;
$app-template-engine = ::('Template6').new;
$app-template-engine.add-path: $path;
my $module = "Web::App::Ballet::Template::$name";
$app-template-engine = ::($module).new;
$app-template-engine.set-path: $path;
}

sub use-tal (Str $path = './templates') is export
sub use-template6 (Str $path = './views') is export
{
require Flower::TAL;
$app-template-engine = ::('Flower::TAL').new;
$app-template-engine.provider.add-path: $path;
require Web::App::Ballet::Template::Template6;
set-template-engine('Template6', $path);
}

sub use-mojo (Str $path = './views') is export
{
require Web::App::Ballet::Template::Mojo;
set-template-engine('Mojo', $path);
}

sub use-tal (Str $path = './views') is export
{
require Web::App::Ballet::Template::TAL;
set-template-engine('TAL', $path);
}

sub use-html (Str $path = './views') is export
{
require Web::App::Ballet::Template::HTML;
set-template-engine('HTML', $path);
}

## TODO: support Plosurin.

sub use-transport ($object) is export
{
$app-transport = $object;
Expand All @@ -51,6 +70,7 @@ sub template-engine is export
{
if ! $app-template-engine.defined
{
## We default to Template6 is left unspecified.
use-template6;
}
return $app-template-engine;
Expand Down Expand Up @@ -113,26 +133,14 @@ sub delete (Pair $route) is export
handle-route($route, 'DELETE');
}

sub template (Str $template, *%opts)
sub template (Str $template, *%named, *@positional)
{
my $te = template-engine;
if $te.can('process')
{
return $te.process($template, |%opts);
}
elsif $te.can('get')
{
my $tmpl = $te.get($template);
if $tmpl.can('render')
{
return $tmpl.render(|%opts);
}
}

die "template engine is not supported.";
$te.render($template, |%named, |@positional);
}

sub dance is export
{
app.run;
}

6 changes: 6 additions & 0 deletions lib/Web/App/Ballet/Template.pm6
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
role Web::App::Ballet::Template
{
method render ($template, *%named, *@positional) { ... }
method set-path ($template-path) { ... }
}

19 changes: 19 additions & 0 deletions lib/Web/App/Ballet/Template/HTML.pm6
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Web::App::Ballet::Template::HTML does Web::App::Ballet::Template
{
use HTML::Template;

has $!engine = HTML::Template;
has $!path = './views';

method render ($template-name, *%named, *@positional)
{ ## HTML::Template uses named parameters.
my $template = $!path ~ $template-name;
$!engine.from_file($template).with_params(%named).output;
}

method set-path ($template-path)
{
$!path = $template-path;
}

}
19 changes: 19 additions & 0 deletions lib/Web/App/Ballet/Template/Mojo.pm6
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Web::App::Ballet::Template::Mojo does Web::App::Ballet::Template
{
use Template::Mojo;

has $!engine = Template::Mojo;
has $!path = './views';

method render ($template-name, *%named, *@positional)
{ ## Template::Mojo uses positional paramemters.
my $template = slurp($!path ~ $template-name);
$!engine.new($template).render(|@positional);
}

method set-path ($template-path)
{
$!path = $template-path;
}

}
17 changes: 17 additions & 0 deletions lib/Web/App/Ballet/Template/TAL.pm6
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Web::App::Ballet::Template::TAL does Web::App::Ballet::Template
{
use Flower::TAL;

has $!engine = Flower::TAL.new;

method render ($template, *%named, *@positional)
{ ## Flower::TAL uses named parameters only.
$!engine.process($template, |%named);
}

method set-path ($template-path)
{
$!engine.provider.add-path($path);
}

}
17 changes: 17 additions & 0 deletions lib/Web/App/Ballet/Template/Template6.pm6
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Web::App::Ballet::Template::Template6 does Web::App::Ballet::Template
{
use Template6;

has $!engine = Template6.new;

method render ($template, *%named, *@positional)
{ ## Template6 uses named parameters only.
$!engine.process($template, |%named);
}

method set-path ($template-path)
{
$!engine.add-path($path);
}

}
2 changes: 0 additions & 2 deletions test/example1.p6 → test/simple.p6
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use lib './lib';

use Web::App::Ballet;

use-scgi; ## HTTP::Easy is currently broken.

get '/' => sub ($c)
{
$c.content-type: 'text/plain';
Expand Down

0 comments on commit 6349658

Please sign in to comment.