Use LESS in MODx
First of all, download the LESS compiler in PHP – lessphp. In fact, of the whole distribution, we need only one file – lessc.inc.php we sviashchennogo extracted from the archive and placed in some directory in assets, for example, in the assets/libs.
Next step, create in the admin panel of MODx snippet, call it somehow I set the date, for example, includeLess. Well then it's simple, I will give a snippet code. Code is commented, so deal with it easier.
/* the path to the assets directory */
$rbBaseDir = $modx->config['rb_base_dir'];
/* plug-in the desired class */
require_once($rbBaseDir.'libs/lessc.inc.php');
if (isset($style)) { // if the snippet passed parameter style
/* directory LESS files */
$lessDir = $rbBaseDir.$templateLessDir;
/* form the path to the connect LESS file (the style parameter) */
$lessDir = $lessDir.$style.'.less';
/* path to CSS cache (compiled LESS) */
$compiledCssDir = 'cache/css/';
if (file_exists($lessCssPath)) { // check the existence of LESS-file
/* create an object refers to the LESS parser and configure it */
$lessParser = new lessc();
$lessParser->importDir = $lessDir;
the
- /*
* read the contents of the plug-LESS file
* and compile it into the variable $cssSource the
- */
$lessSource = file_get_contents($lessCssPath);
$cssSource = $lessParser->parse($lessSource);
the
- /*
* writable in the compiled CSS relative path
* files (there are all sorts of background-image, etc.) the
- */
$cssSource = str_replace('url('.$urlsRewriteMatch,
'url('.$urlsRewriteReplace.')',
$cssSource);
the
- /*
* get the MD5 hash of the compiled CSS code the
- */
$cssSourceHash = md5($cssSource);
$compiledCssPath = $rbBaseDir.$compiledCssDir.$cssSourceHash.'.css';
the
- /*
* check for the existence of the cache file,
* if not - create it the
- */
if (!file_exists($compiledCssPath)) {
file_put_contents($compiledCssPath, $cssSource);
}
/* return the path to the CSS file */
return 'assets/cache/css/'.$cssSourceHash.'.css';
}
}
The snippet has a single parameter – the style that defines the name of the plug-LESS file (without the extension .less (extension is automatically supplied), i.e. in the case of the connection file.less, call the snippet like [!includeLess? &style=`file`!]).
The snippet returns the text string is the path to the CSS file. A sample application snippet:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css"
media=screen href="[!includeLess? &style=`style`!]" /> the
- ...
After calling our MODx snippet includeLess, the page code becomes:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css"
media=screen
href="assets/cache/css/f8c99b14586d8920b693c8cc7f0da6a1.css" /> the
- ...
Next, go into the properties of the snippet in the line "default Settings" V line:
&templateLessDir=Directory LESS files;text;templates/template/less/ &urlsRewriteMatch=the Substring to search for if you are overwriting the relative addresses;text;imgs/ a &urlsRewriteReplace=the Substring to replace when overwriting the relative addresses;text;../../templates/template/imgs/
Persistent, see that appears, fields, parameters. Fill in your own values.
Do not forget to create the directory assets/cache/css and assign it write permissions.
Комментарии
Отправить комментарий