Skip to content

Instantly share code, notes, and snippets.

@maximevalette
Created September 26, 2011 21:16
Show Gist options
  • Save maximevalette/1243426 to your computer and use it in GitHub Desktop.
Save maximevalette/1243426 to your computer and use it in GitHub Desktop.
LESS examples, functions and PHP cache
# LESS CSS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^css/(.+).css$ /less.php?route=css/$1.less [L,QSA]
#header {
color: #4D926F;
border-left: 1px;
border-right: 2px;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
}
#header h1 {
font-size: 26px;
font-weight: bold;
}
#header p {
font-size: 12px;
}
#header p a {
text-decoration: none;
}
#header p a:hover {
border-width: 1px;
}
#footer {
border-radius: 10px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
}
h2 {
color: #4D926F;
}
/* Variables */
@color: #4D926F;
@the-border: 1px;
/* Fonctions */
.rounded-corners (@radius: 5px) {
border-radius: @radius;
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
}
/* CSS imbriqué */
#header {
color: @color;
border-left: @the-border;
border-right: @the-border * 2;
.rounded-corners;
h1 {
font-size: 26px;
font-weight: bold;
}
p { font-size: 12px;
a { text-decoration: none;
&:hover { border-width: 1px }
}
}
}
#footer {
.rounded-corners(10px);
}
h2 {
color: @color;
}
.rounded_borders (@radius: 4px) {
-moz-border-radius: @radius;
-webkit-border-radius: @radius;
-o-border-radius: @radius;
border-radius: @radius;
}
.default_font () {
font-family: "Lucida Grande","Trebuchet MS",sans-serif;
-webkit-font-smoothing: antialiased;
font-size: 12px;
color: @default_color;
}
.box_shadow (@shadow: 0px 1px 2px @gray_hard) {
-moz-box-shadow: @shadow;
-webkit-box-shadow: @shadow;
-o-box-shadow: @shadow;
box-shadow: @shadow;
}
.text_shadow (@shadow: #ffffff) {
text-shadow: 1px 1px @shadow;
}
<?php
/**
*
* Parse les fichiers LESS en CSS à la volée.
*
*/
header('Content-Type: text/css');
error_reporting(0);
/* Si le fichier existe pas, on va pas plus loin */
if (!file_exists(dirname(__FILE__).'/'.$_GET['route']) || !preg_match('/\.(less|css)$/i',$_GET['route'])) die;
/* On va voir s'il y a du cache, déjà */
$cache_file = dirname(__FILE__) . '/data/cache/'.md5($_GET['route']);
/* On regarde s'il y a des fichiers importés dans le CSS (ne supporte qu'un niveau) */
$orig_less = file_get_contents(dirname(__FILE__).'/'.$_GET['route']);
preg_match_all('/@import \'(.+)\';/',$orig_less,$r);
$last_date = (int)filemtime(dirname(__FILE__).'/'.$_GET['route']);
foreach ($r[1] as $file) {
$mtime = (int)@filemtime(dirname(__FILE__).'/'.dirname($_GET['route']).'/'.$file.'.less');
if ($mtime > $last_date) {
$last_date = $mtime;
}
}
/* S'il n'y en a pas ou qu'il est trop vieux : Génération et sauvegarde du LESS */
if (!file_exists($cache_file) || (int)filemtime($cache_file) < $last_date) {
include('lessc.inc.php');
$lc = new lessc(dirname(__FILE__).'/'.$_GET['route']);
$css = $lc->parse();
@unlink($cache_file);
file_put_contents($cache_file,$css);
echo $css;
} else {
echo file_get_contents($cache_file);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment