Module to parse LESS files via PHP.

RockLESS

Support forum thread: https://processwire.com/talk/topic/20658-rockless-php-less-parsing-helper-module/

This is a helper module for parsing LESS and returning the resulting CSS file or content. It uses the wikimedia less parser. See the docs here: https://github.com/wikimedia/less.php

Beside the methods provided by RockLESS you can call any of the underlying methods of the less parser. You can retrieve an instance of the parser easily:

$less = $modules->get('RockLESS');
$options = []; // optional
$parser = $less->getParser($options);
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();

Parsing LESS files


Parsing a single file can be done via $less->parse() or its alias $less->parseFile(). But you can also parse multiple LESS files into one single CSS file:

$less = $modules->get('RockLESS');
$dir = $config->paths->assets;
$options = []; // optional, see docs at saveCSS method
$css = $less->parseFiles([
  $dir."foo.less",
  $dir."bar.less",
], $options);

This will always return the parsed CSS content ready for writing it to a file or to an inline <style> tag. This method will ALWAYS parse the files you throw into it, no matter if anything has changed or not. If you want to cache the results you can use the saveCSS method:

$less = $modules->get('RockLESS');
$dir = $config->paths->assets;
$options = []; // optional
$css = $config->paths->templates . "main.css";
$result = $less->saveCSS($css, [
  $dir."foo.less",
  $dir."bar.less",
], $options);

The method will return a WireData object holding the path and the url of the generated CSS file and the parsed CSS content:

img

The options array can hold the following settings:

  • parserOptions: Options for the less parser.
  • monitorFiles: Array of files to monitor for changes.
  • monitorDirs: Array of directories to monitor for changes.
  • monitorDirDepth: Depth of directories to monitor for changes.

Example:

$less = $modules->get('RockLESS');
$dir = $config->paths->assets;
$css = $config->paths->templates . "main.css";
$result = $less->saveCSS($css, [
  $dir."foo.less",
  $dir."bar.less",
], [
  'parserOptions' => ['compress' => true],
  'monitorFiles' => [
    $config->paths->assets."included-by-foo.less",
    $config->paths->assets."included-by-bar.less",
  ],
  'monitorDirs' => $config->paths->templates,
  'monitorDirDepth' => 3, // default is 0
]);

Use LESS files in your modules


Using RockLESS it is very easy to write your module's styles as LESS and not CSS. RockLESS has a addToConfig() method that will automatically parse a LESS file and add it to the $config->styles array. The css file will only be generated when it does not exist or the less file has changed:

// in init() method
$url = $this->config->urls($this);
$file = $url.$this->className.".less";
$less = $this->modules->get('RockLESS'); /** @var RockLESS $less */
if($less) $less->addToConfig($file);
else $this->config->styles->add("$file.css");

Sending PHP data/variables to LESS


Sometimes you might need to send PHP variables to your LESS files. For example you could want to create a stylesheet based on some user input.

$less = $modules->get('RockLESS');
$less->vars = [
  'foo' => 'bar',
];
$css = $less->getCSS($config->paths->templates . 'less/theme.less')->cssUrl;
echo "<link rel='stylesheet' type='text/css' href='$css'>";

Install and use modules at your own risk. Always have a site and database backup before installing new modules.

Twitter updates

  • ProcessWire 3.0.187: This week we have some very useful new additions to both the core Repeater Fieldtype and the ProFields Repeater Matrix Fieldtype. This post covers all the details along with a couple of brief demonstration videos— More
    22 October 2021
  • ProcessWire 3.0.185 (dev) core updates, plus new Session Allow module— More
    17 September 2021
  • Three new ProcessWire Textformatter modules: Find/Replace, Markdown in Markup, and Emoji— More
    3 September 2021

Latest news

  • ProcessWire Weekly #389
    The 389th issue of ProcessWire Weekly we'll check out the latest core updates, introduce the Session Viewer module, and more. Read on!
    Weekly.pw / 23 October 2021
  • New Repeater and Repeater Matrix features
    This week we have some very useful new additions to both the core Repeater Fieldtype and the ProFields Repeater Matrix Fieldtype. This post covers all the details along with a couple of brief demonstration videos. 
    Blog / 22 October 2021
  • Subscribe to weekly ProcessWire news

“ProcessWire is like a breath of fresh air. So powerful yet simple to build with and customise, and web editors love it too.” —Margaret Chatwin, Web developer