:cake: Less parser plugin for CakePHP

Overview

Less parser plugin for CakePHP 3.X

Build status Code coverage License Latest Stable Version Total Downloads Code Climate

This plugin has a helper to help you parsing .less files in CakePHP 3.0 applications.

By default, the helper will parse all less files to CSS files using less.php but if, for any reason, it fails, will fallback to the less.js parser both included by this plugin (this way you'll see any errors on screen).

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require elboletaire/less-cake-plugin

After installing it you'll need to load it on your bootstrap.php file:

Plugin::load('Less');

And load it into your (App)Controller:

public $helpers = ['Less.Less'];

Usage

By default it will compress files using the php parser with cache enabled. This will fill your css folder with a bunch of files starting with lessphp_ used for the cache. I recommend you adding these files to your .gitignore file in order to prevent commiting them:

lessphp_*

Basically, you give the helper a less file to be loaded (usually from /less directory) and it returns the html link tag to the compiled CSS:

echo $this->Less->less('less/styles.less');
// will result in something like...
<link rel="stylesheet" href="/css/lessphp_8e07b9484a24787e27f9d71522ba53443d18bbd2.css" />

You can compile multiple files if you pass an array:

echo $this->Less->less(['less/myreset.less', 'less/styles.less']);
// They will be compiled in the same file, so the result will be the same as the previous one
<link rel="stylesheet" href="/css/lessphp_e0ce907005730c33ca6ae810d15f57a4df76d330.css"/>

Or you can load your less files with the HtmlHelper sending the files to the CSS block:

$this->Html->css('/less/styles.less?', ['block' => true, 'rel' => 'stylesheet/less']);

Note the ? at the end of the filename. It's used to force the .less extension. Otherwise it would be overwriten to styles.less.css by the UrlHelper.

And then parse all your files using the fetch method:

echo $this->Less->fetch();
echo $this->fetch('css');

By default, any less file found will be removed from the css block and thus, not printed.

You can also pass any option to both less.js and less.php parsers:

echo $this->Less->less('less/styles.less', [
    'js' => [
        // options for lessjs (will be converted to a json object)
    ],
    'parser' => [
        // options for less.php parser
    ],
    // The helper also has its own options
]);
// Same if using the fetch method, but the options are as first param:
echo $this->Less->fetch(['js' => []]);

If you want to use the less.js parser directly, instead of just as a fallback, or you want to use the #!watch method, you can do it so by setting the js parser to development:

echo $this->Less->less('less/styles.less', ['js' => ['env' => 'development']]);

This will output all the links to the less files and the needed js files to parse the content only using the less.js parser.

Note: if there is an error in the php parser the helper will fallback to the js parser so you can see the errors in screen. If, for any reason, you can't see those errors, check the error.log file in the logs folder; it will contain any error generated by the less.php parser.

To load less files inside plugins you can use plugin notation:

echo $this->Less->less('Bootstrap.less/styles.less');
// or...
echo $this->Less->less('/Bootstrap/less/styles.less');
// both will load plugins/Bootstrap/webroot/less/styles.less file

Options

Beside the options for less.js and less.php parsers you can set three options to the helper:

  • cache: default's to true. If disabled, the output will be raw CSS wrapped with <style> tags.
  • tag: default's to true. Whether or not return the code with its proper tag (with cache enabled will be a link tag, whilst without cache will be a style tag).
  • less: default's to /bootstrap/js/less.min. You can use this var to set a custom less.js file.
// Get the link to the resulting file after compressing
$css_link = $this->Less->less('less/styles.less', [
    'tag'   => false
]);

// Get the compiled CSS (raw)
$compiled_css = $this->Less->less('less/styles.less', [
    'cache' => false,
    'tag'   => false
]);

As a default setting of the LessHelper, all the CSS generated by the less.php parser is compresed. To override this set compress to false in the less.php parser options:

echo $this->Less->less('less/styles.less', [
  'parser' => ['compress' => false]
]);

Modify Vars

Last but not least, if you want to overwrite any variable set on your less files you can use the $modify_vars param:

echo $this->Less->less(['less/styles.less'], [], ['bgcolor' => 'magenta']);
echo $this->Less->fetch([], ['bgcolor' => 'magenta']);
// Will overwrite any @bgcolor found in styles.less to #f0f

License

Copyright 2015 Òscar Casajuana (a.k.a. elboletaire)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
imitations under the License.
Comments
  • CakePHP - plugins directory change

    CakePHP - plugins directory change

    Hello, it's me again. :)

    The CakePHP guys changed the plugin's directory in their latest commit. Now every plugin is saved into the vendor folder. So your plugin is working without problems, but only if I copy the LessHelper.php into \View\Helper directory.

    If you will have some time, maybe you can look into it. :) Thanks!

    opened by Perzonallica 6
  • Path to

    Path to "less" files

    Hi,

    I have a little problem that I can't solve, when I try to pull my compiled less files in the css block, I got an error.

    Here are the folders where are my files :

    --webroot / ----css / ------style.css ------other.css ------less / --------main.less ----js/ ----img/

    Here is the code I use :

    echo $this->Html->css('less/main.less?', ['block' => true, 'rel' => 'stylesheet/less']); echo $this->Less->fetch(); // it seems the problem comes from here echo $this->fetch('css');

    And the error :

    FileError: 'http://localhost/my_project/my_project/css/less/main.less' wasn't found (404)

    The correct path is, obviously, this one :

    http://localhost/my_project/css/less/main.less

    For a reason that I can't find, "my_project" is duplicated in the path.

    question 
    opened by Zat42 3
  • added options to be passed on to Html->css

    added options to be passed on to Html->css

    Hi,

    like discussed in #8, CakePdf needs full urls. So I added a parameter options_for_css to be passed to Html->css.

    I'm not this familiar with php, so I can't provide any tests.

    regards dieter

    opened by dspaeth-faber 3
  • fullBase Url

    fullBase Url

    Hi,

    I try to generate a css URL with full base. How can I get this to work?

    $this->Less->less('less/pdf.less', ['fullBase' => true]);
    

    does not work.

    thanks & regards Dieter

    enhancement accepting merge requests 
    opened by dspaeth-faber 3
  • Generating fullBase urls conflicts with cache

    Generating fullBase urls conflicts with cache

    Using fullBase urls generates issues if you're using cache and your app works on both SSL and non-SSL environment.

    Disabling the fullBase setting should fix this issue.

    bug 
    opened by elboletaire 0
  • CakePHP 3.0 - install on stable version

    CakePHP 3.0 - install on stable version

    $ composer require elboletaire/less-cake-plugin

    Using version dev-master for elboletaire/less-cake-plugin ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages.

    Problem 1 - Can only install one of: cakephp/cakephp[3.0.x-dev, 3.0.0]. - Can only install one of: cakephp/cakephp[3.0.x-dev, 3.0.0]. - elboletaire/less-cake-plugin dev-master requires cakephp/cakephp 3.0.x-dev -> satisfiable by cakephp/cakephp[3.0.x-dev]. - Installation request for elboletaire/less-cake-plugin dev-master -> satisfiable by elboletaire/less-cake-plugin[dev-master]. - Installation request for cakephp/cakephp == 3.0.0.0 -> satisfiable by cakephp/cakephp[3.0.0].

    Installation failed, reverting ./composer.json to its original content.

    opened by coolex 0
  • Is there a way to remove compiled css files?

    Is there a way to remove compiled css files?

    Not sure whether I can configure Less helper to remove old compiled css files.

    At the moment, I have written my own tool. Would be OK for an APP but absolutely inconvenient for plugins.

    opened by philsweb 0
  • fix for ['parser']['cache_dir'] and ['css']

    fix for ['parser']['cache_dir'] and ['css']

    additional parameter ['css'] allows for passing to $this->Html->css()

    Corrected processing of ['parser']['cache_dir'] parameter to allow for $this->Html->css() to generate links with the correct path.

    opened by dwilbanks 1
  • compile function returns base filename, not relative

    compile function returns base filename, not relative

    When using an optional $options['cache_dir'] parameter the CSS file that gets created will be created in that directory instead of the CSS directory.

    However the \Less_Cache::Get function returns the basename() of the generated file.

    The result of this problem is that the filename that gets passed to $this->Html->css a basename() and the filename that gets generated by \Less_Cache::Get is inside the ['cache_dir'] directory.

    There are 3 scenarios.

    1. No $options['parser']['cache_dir']
    2. $options['parser']['cache_dir'] that begins with / relative to WWW_ROOT
    3. $options['parser']['cache_dir'] that does not begin with / relative to /css

    The $this->compile() function looks like this:

    if ($cache) {
       if( !isset($options['cache_dir'])){
          $options['cache_dir'] =$this->cssPath;
       } else if( substr( $options['cache_dir'], 0, 1 ) =='/'){
          $options['cache_dir']  = WWW_ROOT . $options['cache_dir'];
       } else {
          $options['cache_dir']  = trim($this->cssPath, '/') . $options['cache_dir'];
       }
       return \Less_Cache::Get($parse, $options, $modifyVars);
    }
    

    Inside the $this->less() function looks like this:

       $cache_dir = isset($options['parser']['cache_dir']) ? $options['parser']['cache_dir'] : "";
       $css = $cache_dir . $this->compile($less, $options['cache'], $options['parser'], $modifyVars);
    
    bug 
    opened by dwilbanks 6
  • No way to add CSS Options

    No way to add CSS Options

    When Html->css is called on line 157, there is no way to set options.

    I've made this small change which allows for setting Html->css options.

    $cssOpt = isset($options['cssopt'] )?$options['cssopt']:[]; return $this->Html->css($css,$cssOpt);

    opened by dwilbanks 5
Releases(v1.7.1)
  • v1.7.1(May 6, 2016)

    The fullBase setting was causing issues for people with sites working both with and without SSL.

    Due to the fullBase setting, and with cache enabled, the cached files were generated with incorrect URLs.

    Source code(tar.gz)
    Source code(zip)
  • v1.7.0(Apr 28, 2016)

    Internal coding changes.

    This is a major upgrade because now compile and jsBlock methods are protected.

    You should not be using those methods, use less instead. You can use all the capabilities from compile and jsBlock methods using the less method.

    Source code(tar.gz)
    Source code(zip)
  • v1.6.2(Nov 21, 2015)

  • v1.6.1(Mar 23, 2015)

  • v1.6.0(Mar 1, 2015)

    Now you can fetch your less files sent to the css block and parse them directly:

    // in your views..
    echo $this->Html->css('Bootstrap.less/bootstrap.less?', ['rel' => 'stylesheet/less', 'block' => true]);
    // [...]
    echo $this->Html->css('less/styles.less?', ['rel' => 'stylesheet/less', 'block' => true]);
    
    // And then, in your template:
    echo $this->Less->fetch(); // will compile any less file found on the css view block
    echo $this->fetch('css'); // will print remaining css files
    

    You were currently able to use plugin notation to load your less files. In this version you're now also able to load files using "slashes notation" (/plugin/less/file.less).

    Source code(tar.gz)
    Source code(zip)
  • v1.5.0(Feb 21, 2015)

    Tests have been added to this release. Thanks to them I've found some bugs that I've successfuly bugfixed.

    If you would like to test the plugin cd into the plugin directory, run composer install.

    Then run phpunit into that folder after composer has finished installing. If you don't have phpunit you can easily add it with composer require phpunit/phpunit.

    And then run php bin/phpunit inside the plugin directory.

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Jan 12, 2015)

Owner
Òscar Casajuana
DevOps, full-stack developer
Òscar Casajuana
CakePHP Minify HTML Plugin

MinifyHtml CakePHP 4, HTML Minify Plugin Installation To install via Composer, use the command below, it will automatically detect the latest version

Cees-Jan Kiewiet 19 Dec 28, 2021
Provides helpers functions for CakePHP to use Laravel Mix.

AssetMix plugin for CakePHP Provides integration with your CakePHP application & Laravel Mix. This branch works with CakePHP 4.0+, see version map for

Ishan Vyas 27 Nov 19, 2022
A Parser for CSS Files written in PHP. Allows extraction of CSS files into a data structure, manipulation of said structure and output as (optimized) CSS

PHP CSS Parser A Parser for CSS Files written in PHP. Allows extraction of CSS files into a data structure, manipulation of said structure and output

Raphael Schweikert 1.6k Jan 5, 2023
An ultimate troll plugin for PocketMine-MP

LMAO An ultimate troll plugin for PocketMine-MP Feature Feature Description Alone Hides every player, let them feed alone Burn Burn the player FakeOp

Ngọc Lam 5 Apr 14, 2022
Rafa Cake and Bakery is a web-based application project that aims to introduce Rafa Cake and Bakery, introduce what products are sold and can also order them via Whatsapp.

Rafa-cake-and-bakery Rafa Cake and Bakery is a web-based application project that aims to introduce Rafa Cake and Bakery, introduce what products are

Aan Evian Nanda 2 Jun 19, 2022
[READ-ONLY] Collection library in CakePHP. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Collection Library The collection classes provide a set of tools to manipulate arrays or Traversable objects. If you have ever used underscore

CakePHP 85 Nov 28, 2022
[READ-ONLY] The event dispatcher library for CakePHP. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Event Library This library emulates several aspects of how events are triggered and managed in popular JavaScript libraries such as jQuery: An

CakePHP 21 Oct 6, 2022
[READ-ONLY] Validation library from CakePHP. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Validation Library The validation library in CakePHP provides features to build validators that can validate arbitrary arrays of data with eas

CakePHP 39 Oct 11, 2022
[READ-ONLY] CakePHP Utility classes such as Inflector, Text, Hash, Security and Xml. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Utility Classes This library provides a range of utility classes that are used throughout the CakePHP framework What's in the toolbox? Hash A

CakePHP 112 Feb 15, 2022
[READ-ONLY] Easy to use Caching library with support for multiple caching backends. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Caching Library The Cache library provides a Cache service locator for interfacing with multiple caching backends using a simple to use interf

CakePHP 49 Sep 28, 2022
[READ-ONLY] A flexible, lightweight and powerful Object-Relational Mapper for PHP, implemented using the DataMapper pattern. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP ORM The CakePHP ORM provides a powerful and flexible way to work with relational databases. Using a datamapper pattern the ORM allows you to m

CakePHP 146 Sep 28, 2022
Cakephp-book allows you to search in the official CakePHP documentation directly from the console.

CakeDC/Book plugin for CakePHP cakephp-book allows you to search in the official CakePHP documentation directly from the console. Requirements CakePHP

Cake Development Corporation 3 Apr 13, 2022
A sample CakePHP api application using CakeDC/cakephp-api and swoole as server

CakePHP Application Skeleton composer create-project --prefer-dist cakephp/app Added sample data using https://github.com/annexare/Countries Created m

Marcelo Rocha 3 Jul 28, 2022
Sslurp is a simple library which aims to make properly dealing with SSL in PHP suck less.

Sslurp v1.0 by Evan Coury Introduction Dealing with SSL properly in PHP is a pain in the ass and completely insecure by default. Sslurp aims to make i

Evan Coury 65 Oct 14, 2022
Laravel CRUD Generator, Make a Web Application Just In Minutes, Even With Less Code and fewer Steps !

?? CRUDBOOSTER - Laravel CRUD Generator Laravel CRUD Generator, Make a Web Application Just In Minutes, Even With Less Code and fewer Steps ! About CR

Crocodic Studio 1.7k Jan 8, 2023
Scripts, helpers and configs to make hacking on Synack less painful

SynackMoPleasure Scripts, helpers and configs to make hacking on Synack less painful TuPOC HTTP OOB PHP Logger This PHP script was created to test/exp

Osirys 3 Sep 20, 2022
This Slim Framework middleware will compile LESS CSS files on-the-fly using the Assetic library

This Slim Framework middleware will compile LESS CSS files on-the-fly using the Assetic library. It supports minification and caching, also via Asseti

Gerard Sychay 22 Mar 31, 2020
Code generation with logic-less templates for Yii

Caviar Code generation with logic-less templates for Yii. Caviar vs Gii You might be wondering why you should use Caviar instead of Gii, so let us tak

Christoffer Niska 10 Dec 19, 2015
Web page performance/seo/security/accessibility analysis, browser-less for PHP

Web page performance/seo/security/accessibility analysis, browser-less for PHP

Lightship 5 Dec 15, 2022
DBML parser for PHP8. It's a PHP parser for DBML syntax.

DBML parser written on PHP8 DBML (database markup language) is a simple, readable DSL language designed to define database structures. This page outli

Pavel Buchnev 32 Dec 29, 2022