package Mojolicious::Plugin::SassRenderer;

our $VERSION = '0.02';

use warnings;
use strict;

use Mojo::Base 'Mojolicious::Plugin';
use IO::File;
use Text::Sass;

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

  $options ||= {};

  # Add "sass" handler
  $app->renderer->add_handler(sass => sub {
      my ($r, $c, $output, $options) = @_;

      # Read the Sass file
      my $fname = $r->template_path($options);
      my $fh    = IO::File->new($fname, 'r') or die "Couldn't open $fname: $!";
      my $sass  = do{ local $/ = undef; <$fh>; };
      undef $fh;

      # Generate CSS
      my $oSass = Text::Sass->new;
      my $css   = $oSass->sass2css($sass);

      $$output  = $css;

1; # End of Mojolicious::Plugin::SassRenderer

=head1 NAME

Mojolicious::Plugin::SassRenderer - Sass Renderer Plugin for Mojolicious


Renders Sass files into CSS for your Mojolicious web-apps

  package MyApp;
  use Mojo::Base 'Mojolicious';
  sub startup {
      $self = shift;


  # template
  <!doctype html><html>
      <style type="text/css">
        <%== include 'stylesheets/main', format => 'txt', handler => 'sass' %>

  # sass: MOJO_HOME/templates/stylesheets/main.txt.sass
  $menuColor: #eee

    background-color: $menuColor
    width: 75%


Takes Sass formatted files and renderers them in CSS for your web-app.

=head1 VERSION

Version 0.02

=head1 AUTHOR

Byron Hammond, C<< <byron_hammond <at>> >>

=head1 BUGS

Since this simply uses Text::Sass, it's limited by it's bugs.

Please report any bugs or feature requests to C<bug-mojolicious-plugin-sassrenderer at>, or through
the web interface at L<>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

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

    perldoc Mojolicious::Plugin::SassRenderer

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker


=item * AnnoCPAN: Annotated CPAN documentation


=item * CPAN Ratings


=item * Search CPAN




Sebastian Riedel <sri> for Mojolicious and the suggestion to put this on CPAN


Copyright 2011 Byron Hammond.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See for more information.