Skip to content

Instantly share code, notes, and snippets.

@richthegeek
Created August 13, 2012 15:34
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richthegeek/3341930 to your computer and use it in GitHub Desktop.
Save richthegeek/3341930 to your computer and use it in GitHub Desktop.
Automatic SASS/SCSS compilation with PHPSass and Apache2
Action compile-sass /git/phpsass/compile-apache.php
AddHandler compile-sass .sass .scss
These two files can be used to automatically compile files using PHPSass; It assumes that you place compile-apache.php in the same dir as SassParser.php (the root of PHPSass) and that PHPSass lives in /WEBROOT/git/phpsass.
<?php
header('Content-type: text/css');
require_once './SassParser.php';
function warn($text, $context) {
print "/** WARN: $text, on line {$context->node->token->line} of {$context->node->token->filename} **/\n";
}
function debug($text, $context) {
print "/** DEBUG: $text, on line {$context->node->token->line} of {$context->node->token->filename} **/\n";
}
$file = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['PATH_INFO'];
$syntax = substr($file, -4, 4);
$options = array(
'style' => 'expanded',
'cache' => FALSE,
'syntax' => $syntax,
'debug' => FALSE,
'callbacks' => array(
'warn' => 'warn',
'debug' => 'debug'
),
);
// Execute the compiler.
$parser = new SassParser($options);
try {
print "\n\n" . $parser->toCss($file);
} catch (Exception $e) {
print $e->getMessage();
}
@richthegeek
Copy link
Author

Forgot to mention, you need mod_actions for this to work:

sudo a2enmod actions && sudo service apache2 restart

@davidfells
Copy link

Getting "The requested URL /lib/phpsass/compile-apache.php/api/ajax/css/main.scss was not found on this server.". Note that I put my files in WEBROOT/lib , not WEBROOT/git. SCSS file is in WEBROOT/api/ajax/css/main.scss. Accessing by http://domain/api/ajax/css/main.scss. If I turn off the handler I see the raw SCSS file. Any ideas?

@timbuktuuu
Copy link

hmm this don't work for me its all activated and got the right paths but it does not compiling :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment