Dancer plugins ecosystem

Dancer has been designed to provide the built-in features you need, but to remain slim and not force your choice of technologies, so plugins and engines are an important feature for easy extensibility.

It's easy to write plugins to extend Dancer's DSL and provide various useful extra features. Today, here is a write-up of a small selection of the wide variety of plugins available.

  • Dancer::Plugin::Database

    Covered in more detail already, but Dancer::Plugin::Database provides easy to use and easy to configure DBI database connection management along with a handful of convenience methods.

    It has been designed to make the common stuff as easy as possible.

    A quick example - fetching details of a user and passing them to a template to render a page with details on that user:

    get '/user/:id' => sub {
        my $user = database->quick_select('users', { id => params->{id} });
        return template 'user', $user;
    };

    Calling the database keyword gets you a database handle (which is actually a subclassed DBI::db object, which acts just as a normal DBI database handle but adds a few convenience methods such as quick_select.

  • Dancer::Plugin::DBIC

    If you plan to use DBIx::Class to access your database in an ORM-ish style, then this plugin provides a schema keyword which lets you do things like:

    my @authors = schema->resultset('Author')->search({
        -or => [
            firstname => { like => "%$query%" },
            lastname  => { like => "%$query%" },
        ]
    });

    When configuring the plugin, you can allow DBIC to auto-discover the schema for ultimate ease of use, or you can point it at your own schema classes.

    Covered in more detail in last year's Dancer advent calendar.

  • Dancer::Plugin::Redis

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

    get '/widget/view/:id' => sub {
        template 'display_widget', { widget => redis->get('hash_key'); };
    };
  • Dancer::Plugin::Mongo

    Provides a wrapper around MongoDB. Add the appropriate configuraton options to your config.yml and then you can access a MongoDB database using the 'mongo' keyword:

    get '/widget/view/:id' => sub {
        my $widget = mongo->database->collection->find_one({ id => params->{id} });
    };
  • Dancer::Plugin::Email

    Provides simple email sending for your Dancer applications, powered by Email::Send via an email keyword:

    post '/contact' => sub {
        email {
            to => '...',
            subject => '...',
            message => $msg,
            attach => [ '/path/to/file' ]
        };
    };

    Covered in more detail previously in this year's calendar.

  • Dancer::Plugin::SMS

    Provides simple SMS text messaging sending, powered by SMS::Send:

    post '/sendsms' => sub {
        sms to => '++447......', text => '......';
    };
  • Dancer::Plugin::EscapeHTML

    Makes it easy to escape HTML to help mitigate against XSS vulnerabilities.

    my $encoded = escape_html($some_html);

    In many cases, you'll be passing data to your templates to render, but will never be intentionally passing HTML, only data. In that case, you can arrange for all template params to be escaped before the template receives them by adding the following to config.yml:

    plugins:
        EscapeHTML:
            automatic_escaping: 1
            exclude_pattern: '_html$'

    The exclude_pattern illustrated means that any template param with a name ending in _html will be left untouched.

  • Dancer::Plugin::SimpleCRUD

    Provides simple CRUD support for database tables, with automatic record display, searching, creating, editing, deletion, exporting, etc.

    Covered in more detail previously in this year's calendar.

  • Dancer::Plugin::Ajax

    Provides an ajax keyword to match only requests submitted via AJAX:

    ajax '/check_for_update' => sub {
        # ... some Ajax code
    };
  • Dancer::Plugin::Passphrase

    Makes it easy to securely handle passwords in your Dancer apps, storing and using secure salted hashed passwords with minimal effort.

  • Dancer::Plugin::Cache::CHI

    Provides easy caching using the CHI module, which supports a wide variety of caching backends including memcached and various others. Can cache arbitrary bits of information or entire page responses.

  • Dancer::Plugin::Thumbnail

    Provides easy image thumbnail generation using GD.

  • Dancer::Plugin::Facebook

    Makes it easy to interact with Facebook via Facebook::Graph from your Dancer applications.

  • Dancer::Plugin::Stomp

    Makes it as easy as possible to interact with a Stomp message broker. It provides one new keyword, stomp, which returns a Net::Stomp object.

    post '/messages' => sub {
        stomp->send({ destination => '/queue/foo', body => request->body });
    };

The above is just a small sample - there's plenty more on CPAN.

Just search for Dancer::Plugin::

Why not help people find useful plugins by finding the plugins you use on MetaCPAN and clicking the ++ button?

AUTHOR

David Precious