NAME

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

SYNOPSIS

# In your Dancer application

use Dancer::Plugin::Assets "add_asset";

# Sometime during the request ...

get "/index" => sub { add_asset "/css/beautiful.css"; add_asset "/css/handlebars.js"; };

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

<html>
  <head><title>[% title %]</title>
  [% add_asset("/js/jquery.js") %]
  [% css_tags %]
  </head>
  <body>
  </body>
  [% js_tags %]
</html>

# Or you want all css and js tags inside <head>

<html>
  <head><title>[% title %]</title>
  [% add_asset("/js/jquery.js") %]
  [% add_asset("/js/handlebars.js") %]
  [% add_asset("/css/beautiful.css") %]
  [% 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 var("assets")->export() to generate HTML or var("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 file e.g. config.yml, environments/development.yml or 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"
        minify: 1,
        minified_name: "minified"

METHODS

assets

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

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

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.