The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Dancer::Plugin::Assets - Manage and minify .css and .js assets in a Dancer application

SYNOPSIS

In your Dancer application

 use Dancer::Plugin::Assets qw( assets add_asset );

Sometime during the request ...

 get "/index" => sub {
    ## Include assets by plugin method
    add_asset "/css/beautiful.css";
    add_asset "/css/handlebars.js";

    ## Include assets by assets object
    assets->include( "/css/main.css" );
    assets->include( "/js/something.js" );
 };

Then, in your .tt, print css tags at <head>, print js tags after body

  <html>
    <head><title>[% title %]</title>
    [% css_tags %]
    </head>
    <body>
    </body>
    [% js_tags %]
  </html>

Or you want to add css and js and print them all in template file inside <head>

  <html>
    <head><title>[% title %]</title>
    [% add_asset("/js/jquery.js")         %]
    [% add_asset("/js/handlebars.js")     %]
    [% add_asset("/css/beautiful.css")    %]
    [% CALL assets.include("/js/foo.js")  %]
    [% css_and_js_tags || js_and_css_tags %]
    </head>
    <body>
    </body>
  </html>

DESCRIPTION

Dancer::Plugin::Assets integrates File::Assets into your Dancer application. Essentially, it provides a unified way to include .css and .js assets from different parts of your program. When you're done processing a request, you can use assets->export() or [% css_and_js_tags %] to generate HTML or assets->exports() to get a list of assets.

D::P::Assets will also handle .css files of different media types properly.

In addition, D::P::Assets includes support for minification via YUI compressor, JavaScript::Minifier, CSS::Minifier, JavaScript::Minifier::XS, and CSS::Minifier::XS

Note that Dancer::Plugin::Assets does not serve files directly, it will work with Static::Simple or whatever static-file-serving mechanism you're using.

USEAGE

For usage hints and tips, see File::Assets

CONFIGURATION

You can configure D::P::Assets by manipulating the environment configration files, e.g.

    config.yml
    environments/development.yml
    environments/production.yml

The following settings are available:

    url            # The url to access the asset files default "/"

    base_dir       # A path to automatically look for assets under (e.g. "/public")
                   
                   # This path will be automatically prepended to includes, so that instead of
                   # doing ->include("/public/css/stylesheet.css") you can just do ->include("/css/stylesheet.css")
                   
                   
    output_dir     # The path to output the results of minification under (if any).
                   # For example, if output is "built/" (the trailing slash is important), then minified assets will be
                   # written to "root/<assets-path>/static/..."
                   #
                   # Designates the output path for minified .css and .js assets
                   # The default output path pattern is "%n%-l%-d.%e" (rooted at the dir of <base>)
                   
                   
    minify         # "1" or "best" - Will either use JavaScript::Minifier::XS> & CSS::Minifier::XS or
                   #                 JavaScript::Minifier> & CSS::Minifier (depending on availability)
                   #                 for minification
                   # "0" or "" or undef - Don't do any minfication (this is the default)
                   # "./path/to/yuicompressor.jar" - Will use YUI Compressor via the given .jar for minification
                   # "minifier" - Will use JavaScript::Minifier & CSS::Minifier for minification


    minified_name  # The name of the key in the stash that provides the assets object (accessible via config->{plugins}{assets}{minified_name}.
                   # By default, the <minified_name> is "minified".

Example configuration

Here is an example configuration: ( All the value are set by default )

    plugins:
        Assets:
            base_dir: "/public"
            output_dir: "static/%n%-l.%e"
            minified_name: "minified"
            minify: 1

METHODS IN ROUTE CODE

assets

Return File::Assets object that exists throughout the lifetime of the request

add_asset

same as sub { assets->include( shift ); return undef }

METHODS IN TT

[% assets %]

Return File::Assets object that exists throughout the lifetime of the request

[% add_asset %]

same as [% CALL assets.include( "file..." ) %]

[% css_tags %]

return tags of css in html

[% js_tags %]

return tags of javascript in html

[% css_and_js_tags %]

return tags in this order of css and javascript in html

[% js_and_css_tags %]

return tags in this order of javascript and css in html

AUTHOR

Michael Vu, <micvu at cpan.org>

BUGS

Please report any bugs or feature requests to bug-dancer-plugin-assets at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer::Plugin::Assets. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Dancer::Plugin::Assets

You can also look for information at:

SEE ALSO

File::Assets

Dancer::Plugin

http://developer.yahoo.com/yui/compressor/

JavaScript::Minifier::XS

CSS::Minifier::XS

JavaScript::Minifier

CSS::Minifier

ACKNOWLEDGEMENTS

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.