Use LESS in MODx

need to use LESS MODx, it is very not like every time you change the source LESS file, recompile it manually, with the result that it was decided to send a snippet. What, in fact, and post.

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

  1. /*
  2. * read the contents of the plug-LESS file

    * and compile it into the variable $cssSource the

  3. */
  4. $lessSource = file_get_contents($lessCssPath);

    $cssSource = $lessParser->parse($lessSource);

    the

  5. /*
  6. * writable in the compiled CSS relative path

    * files (there are all sorts of background-image, etc.) the

  7. */
  8. $cssSource = str_replace('url('.$urlsRewriteMatch,

    'url('.$urlsRewriteReplace.')',

    $cssSource);

    the

  9. /*
  10. * get the MD5 hash of the compiled CSS code the

  11. */
  12. $cssSourceHash = md5($cssSource);

    $compiledCssPath = $rbBaseDir.$compiledCssDir.$cssSourceHash.'.css';

    the

  13. /*
  14. * check for the existence of the cache file,

    * if not - create it the

  15. */
  16. 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

  1. ...

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

  1. ...

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.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

The release of the new version of the module modLivestreet 0.3.0-rc

mSearch: search + filter for MODX Revolution

Emulator data from GNSS receiver NMEA