Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added recipe for bundling assets with plugins
  • Loading branch information
kraih committed Feb 13, 2012
1 parent 8db50c5 commit 5815079
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/Mojolicious.pm
Expand Up @@ -316,6 +316,9 @@ L<Mojolicious::Renderer> object. The two main renderer plugins
L<Mojolicious::Plugin::EPRenderer> and L<Mojolicious::Plugin::EPLRenderer>
contain more information.
# Add another "templates" directory
push @{$app->renderer->paths}, '/foo/bar/templates';
=head2 C<routes>
my $routes = $app->routes;
Expand Down Expand Up @@ -359,6 +362,9 @@ session data.
For serving static assets from your C<public> directory, defaults to a
L<Mojolicious::Static> object.
# Add another "public" directory
push @{$app->static->paths}, '/foo/bar/public';
=head2 C<types>
my $types = $app->types;
Expand Down
57 changes: 57 additions & 0 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -616,6 +616,63 @@ to C<CPAN>.
$ make dist
$ mojo cpanify -u USER -p PASS Mojolicious-Plugin-DebugHelper-0.01.tar.gz

=head2 Bundling assets with plugins

Assets such as templates and static files can be easily bundled with your
plugins.

$ mojo generate plugin AlertAssets
$ cd AlertAssets
$ mkdir lib/Mojolicious/Plugin/AlertAssets
$ cd lib/Mojolicious/Plugin/AlertAssets
$ mkdir public
$ echo 'alert("Hello World!");' > public/alertassets.js
$ mkdir templates
$ echo '%= javascript "/alertassets.js"' > templates/alertassets.html.ep

Just append their respective directories to the list of search paths when
your plugin gets loaded.

package Mojolicious::Plugin::AlertAssets;
use Mojo::Base 'Mojolicious::Plugin';

use File::Basename 'dirname';
use File::Spec;
use Mojo::Home;

sub register {
my ($self, $app) = @_;

# Append "templates" and "public" directories to search paths
my $home = Mojo::Home->new(File::Spec->catdir(dirname(__FILE__)));
push @{$app->renderer->paths}, $home->rel_dir('AlertAssets/templates');
push @{$app->static->paths}, $home->rel_dir('AlertAssets/public');
}

1;

Both will work just like normal C<templates> and C<public> direcotries once
you've installed and loaded the plugin, with slightly lower precedence.

use Mojolicious::Lite;

plugin 'AlertAssets';

get '/alert_me';

app->start;
__DATA__

@@ alert_me.html.ep
<!DOCTYPE html>
<html>
<head>
<title>Alert me!</title>
%= include 'alertasset'
</head>
<body>You've been alerted.</body>
</html>

=head2 Custom C<exception> and C<not_found> templates

While the built-in C<exception> and C<not_found> templates are very useful
Expand Down
3 changes: 3 additions & 0 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -366,6 +366,9 @@ Directory to look for layouts in, defaults to C<layouts>.
Directories to look for templates in.
# Add another "templates" directory
push @{$renderer->paths}, '/foo/bar/templates';
=head1 METHODS
L<Mojolicious::Renderer> inherits all methods from L<Mojo::Base> and
Expand Down
3 changes: 3 additions & 0 deletions lib/Mojolicious/Static.pm
Expand Up @@ -198,6 +198,9 @@ Class to use for finding files in C<DATA> section, defaults to C<main>.
Directories to serve static files from.
# Add another "public" directory
push @{$static->paths}, '/foo/bar/public';
=head1 METHODS
L<Mojolicious::Static> inherits all methods from L<Mojo::Base>
Expand Down

0 comments on commit 5815079

Please sign in to comment.