Dancer's ecosystem

Dancer's community is getting more important every day. Today, we will see some useful plugin or engines contributed from the community.

Templates

Dancer::Template::Tenjin

Tenjin is a fast and feature-full templating engine that can be used by Dancer for production purposes. In comparison to Template::Toolkit, it is much more lightweight, has almost no dependencies, and supports embedded Perl code instead of defining its own language.

(On CPAN: http://search.cpan.org/perldoc?Dancer::Template::Tenjin)

Dancer::Template::Tiny

Template::Tiny is an implementation of a subset of Template::Toolkit (the major parts) which takes much less memory and is faster. If you're only using the main functions of Template::Toolkit, you could use Template::Tiny. You can also seamlessly move back to Template::Toolkit whenever you want.

(On CPAN: http://search.cpan.org/perldoc?Dancer::Template::Tiny)

Dancer::Template::Semantic

Template::Semantic is a template engine for XHTML/XML based on XML::LibXML that doesn't use any template syntax. This module takes pure XHTML/XML as a template, and uses XPath or CSS selectors to assign values.

(On CPAN: http://search.cpan.org/perldoc?Dancer::Template::Semantic)

Dancer::Template::Mason

As you could have guessed, this is an interface between Dancer's template engine abstraction layer and the HTML::Mason templating system.

(On CPAN: http://search.cpan.org/perldoc?Dancer::Template::Mason)

Dancer::Template::Xslate

Text::Xslate is a template engine, tuned for persistent applications, safe as an HTML generator, and with rich features.

The concept of Xslate is strongly influenced by Text::MicroTemplate and Template-Toolkit 2, but the central philosophy of Xslate is different from them. That is, the philosophy is sandboxing that the template logic should not have no access outside the template beyond your permission.

(On CPAN: http://search.cpan.org/perldoc?Dancer::Template::Xslate)

Logger

Dancer::Logger::Syslog

This module implements a logger engine that send log messages to syslog, through the Sys::Syslog module.

(On CPAN: http://search.cpan.org/perldoc?Dancer::Logger::Syslog)

Dancer::Logger::PSGI

This class is an interface between your Dancer's application and psgix.logger. Message will be logged in whatever logger you decided to use in your Plack handler. If no logger is defined, nothing will be logged.

(on CPAN/ http://search.cpan.org/perldoc?Dancer::Logger::PSGI)

Dancer::Logger::Spinner

When using this logger and running your application in the terminal, you will see a text spinner running on each message it gets. If you have a page with a lot of request, and they will come in fast, you'll see the spinner running. If you have an app with very little requests in each page or if it is slow, the spinner will run slowly.

(On CPAN: http://search.cpan.org/perldoc?Dancer::Logger::Spinner)

Plugins

Dancer::Plugin::REST

This plugin helps you write a RESTful web service with Dancer. This plugin will be detailed in another article about REST.

(On CPAN: http://search.cpan.org/perldoc?Dancer::Plugin::REST)

Dancer::Plugin::Params::Normalization

This plugin helps you make automatic transformations on requests' parameters, for instance, make them all lowercase. It has easy-to-use defaults, but can do complex and precise normalisation upon configuration.

(On CPAN: http://search.cpan.org/perldoc?Dancer::Plugin::Params::Normalization)

Dancer::Plugin::Mongo

Dancer::Plugin::Mongo provides a wrapper around MongoDB. Add the appropriate configuration options to your config.yml and then you can access a MongoDB database using the mongo keyword.

use Dancer;
use Dancer::Plugin::Mongo;

get '/widget/view/:id' => sub {
    my $widget = mongo->database->table->find_one({ id => params->{id} });
}

(On CPAN: http://search.cpan.org/perldoc?Dancer::Plugin::Mongo)

Dancer::Plugin::Database

Provides an easy way to obtain a connected DBI database handle by simply calling the database keyword within your Dancer application.

use Dancer;
use Dancer::Plugin::Database;

get '/widget/view/:id' => sub {
    my $sth = database->prepare( 'select * from widgets where id = ?' );
    $sth->execute(params->{id});
    template 'display_widget', { widget => $sth->fetchrow_hashref };
};

(On CPAN: http://search.cpan.org/perldoc?Dancer::Plugin::Database)

Dancer::Plugin::DBIC

This plugin provides an easy way to obtain DBIx::Class::ResultSet instances via the the function schema(), which it automatically imports. You just need to point to a DSN in your Dancer configuration file. So you no longer have to write boilerplate DBIC setup code.

use Dancer;
use Dancer::Plugin::DBIC;

get '/profile/:id' => sub {
    my $user = schema->resultset('Users')->find(params->{id});
    template user_profile => { user => $user };
};

(On CPAN: http://search.cpan.org/perldoc?Dancer::Plugin::DBIC)

Dancer::Plugin::SiteMap

Basically, this plugin predefines a pair of routes for you saving you the work in your Dancer application.

The two routes that are defined when called, obtain a list of routes that are in your Dancer application within Dancer's registry. This list is then simply formated appropriately for each route.

For HTML an unordered list containing anchor tags is output within your applications main layout. This can then be styled with CSS to suit.

For the XML site map, it outputs... Yep you guessed it! An XML site map.

As these routes work on a on-demand principle, there is no need to continually update your site map, Dancer::Plugin::SiteMap keeps it up-to-date for you :-)

use Dancer;
use Dancer::Plugin::SiteMap;

That's it! Your Dancer App now has /sitemap and /sitemap.xml routes.

Author

This article has been written by Franck Cuny for the Perl Dancer Advent Calendar 2010.

Copyright

Copyright (C) 2010 by Franck Cuny <franck@lumberjaph.net>, James Ronan <james@ronanweb.co.uk>