I don't have the attributions for where this comes from but it works perfectly for me.
Include this code in your main template file, so it gets run every time.
PseudoCode: If we're on a development server, re-compile the css.
// Auto versioning using PHP, only when on localhost and debugging
if ($this->debug) {
include($_SERVER['DOCUMENT_ROOT'].'/includes/autoCompileLess.php');
auto_compile_less($_SERVER['DOCUMENT_ROOT'].'/css/less/style.less', $_SERVER['DOCUMENT_ROOT'].'/css/styleCompiled.css');
}
This is the code for the autoCompileLess.php. It checks a cache file, and if that file is newer than my main LessCSS file it'll use the cache rather than re-compiling.
PseudoCode: Check the modified times of the cached compiled css, and if that is newer than the main less css file use the cache. Otherwise recompile all the less.
require 'lessc.inc.php';
function auto_compile_less($less_fname, $css_fname) {
$cache = $less_fname;
// We may want to load from the cache
$cache_fname = $less_fname.".cache";
if (file_exists($cache_fname)) {
// Use the cache if the filetime is newer
$cacheTime = filemtime($cache_fname);
$fileTime = filemtime($less_fname);
// If I managed to read both and the cache is still relevant, use it
if ($cacheTime && $fileTime && $fileTime < $cacheTime) {
$cache = unserialize(file_get_contents($cache_fname));
}
}
$new_cache = lessc::cexecute($cache);
if (!is_array($cache) || $new_cache['updated'] > $cache['updated']) {
file_put_contents($css_fname, $new_cache['compiled']);
file_put_contents($cache_fname, serialize($new_cache));
}
}
A bit of googling should get you all the necessary files you need. Perhaps this site, http://leafo.net/lessphp/, would be a good place to start.
Happy coding!
A short blog designed to show all the interesting things I pick up while working with PHP based websites.
Wednesday, 17 October 2012
Friday, 20 July 2012
Code Readability
Or why I use ternary operations but never single line if statements.
This is a massive issue on collaborative projects. And since virtually every single piece of code you use will need to be modified / understood by someone else in the future, in my opinion everything you write should emphasise the readability rather than functionality.
Of course there will always be projects where speed is important, and you want to use the data structure that is fastest / most efficient.
As a professional software engineer (even if it is "just" websites) I am constantly working with other people's code. And it frustrates the hell out of me that people write code that is just so hard to understand.
A ternary operation is fairly trivial for any professional to understand. Even there though, the use of a variable name that explains where it comes from would be so handy!
One thing that annoys me though is single line if statements. Most IDE's allow you to format everything properly according to your conventions (we use Zend), and if everyone follows those it makes it much easier to follow the code when you have to fix a bug. But scanning code by eye for a line (I'm lazy like that) is made so much harder with single line if statements. Especially when you're checking for a value like: $_SESSION['carhire.search.dropoff_location'] .
The number of studies that have been done on the width of columns for readability is astonishing. Made your code more readable by using the following format always:
if (statement) {
then do this
}
And comment your bloody code or I will curse you forever.
This is a massive issue on collaborative projects. And since virtually every single piece of code you use will need to be modified / understood by someone else in the future, in my opinion everything you write should emphasise the readability rather than functionality.
Of course there will always be projects where speed is important, and you want to use the data structure that is fastest / most efficient.
As a professional software engineer (even if it is "just" websites) I am constantly working with other people's code. And it frustrates the hell out of me that people write code that is just so hard to understand.
A ternary operation is fairly trivial for any professional to understand. Even there though, the use of a variable name that explains where it comes from would be so handy!
One thing that annoys me though is single line if statements. Most IDE's allow you to format everything properly according to your conventions (we use Zend), and if everyone follows those it makes it much easier to follow the code when you have to fix a bug. But scanning code by eye for a line (I'm lazy like that) is made so much harder with single line if statements. Especially when you're checking for a value like: $_SESSION['carhire.search.dropoff_location'] .
The number of studies that have been done on the width of columns for readability is astonishing. Made your code more readable by using the following format always:
if (statement) {
then do this
}
And comment your bloody code or I will curse you forever.
Saturday, 14 July 2012
Zend Form, and auto height of a textarea
Zend framework has it's uses. But gee it can be a pain at times.
If you don't have access to the decorators of the form, and you want to style your text areas better, this snippet should come in handy. Stupid scroll bars.
var textArea = "";
jQuery('textarea.info').each(function() {
textArea = jQuery(this);
textArea.removeAttr('rows');
textArea.removeAttr('cols');
textArea.css('height', this.scrollHeight + 'px');
});
If you don't have access to the decorators of the form, and you want to style your text areas better, this snippet should come in handy. Stupid scroll bars.
var textArea = "";
jQuery('textarea.info').each(function() {
textArea = jQuery(this);
textArea.removeAttr('rows');
textArea.removeAttr('cols');
textArea.css('height', this.scrollHeight + 'px');
});
Subscribe to:
Posts (Atom)