PHP Classes

CSS Magic: Compose CSS style sheets programatically

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 335 All time: 7,109 This week: 488Up
Version License PHP version Categories
css-magic 1.0.1Custom (specified...7HTML, PHP 7
Description 

Author

This package can compose CSS style sheets programatically.

The theme class composes style sheets. The selector class defines styles to be added to style sheets. The rule class defines properties to be added to styles.

The theme class assembles all the added styles and returns it as a single string.

Innovation Award
PHP Programming Innovation award nominee
May 2016
Number 11


Prize: One copy of DWebPro Standard License
Usually Web applications use CSS style sheets defined as static files or templates.

However, when your application needs to generate custom CSS that manipulates certain styles and properties, the static CSS approach is not very convenient.

This class provides means to define CSS programmatically by allowing to define style sheets, styles and properties as parameters defined at PHP run time.

Manuel Lemos
Picture of Everton da Rosa
  Performance   Level  
Innovation award
Innovation award
Nominee: 3x

 

Example

<?php
if(isset($_GET['cor'])){
   
$theme = $_GET['cor'];
}else{
   
$theme = 'red';
}

$themes = [];

$themes['red']['background-color'] = '#ff3333';
$themes['red']['color'] = '#ffcccc';
$themes['red']['border-color'] = '#ff0000';

$themes['green']['background-color'] = '#00e600';
$themes['green']['color'] = '#ccffcc';
$themes['green']['border-color'] = '#008000';

$themes['blue']['background-color'] = '#4d94ff';
$themes['blue']['color'] = '#e6f0ff';
$themes['blue']['border-color'] = '#0052cc';

require
'../CSSMagic.php';

$mytheme = new CSSMagic\Theme('My first theme');

$selector1 = new CSSMagic\Selector('body');
$selector1->addRule(new CSSMagic\Rule('color', $themes[$theme]['color']));
$selector1->addRule(new CSSMagic\Rule('background-color', $themes[$theme]['background-color']));

$selector2 = new CSSMagic\Selector('#paragrafo');
$selector2->addRule(new CSSMagic\Rule('font-style', 'italic'));

$selector3 = new CSSMagic\Selector('.myclass');
$selector3->addRule(new CSSMagic\Rule('border', 'thin solid'));
$selector3->addRule(new CSSMagic\Rule('border-color', $themes[$theme]['border-color']));

$mytheme->addSelector($selector1);
$mytheme->addSelector($selector2);
$mytheme->addSelector($selector3);

header("Content-Type: text/css");

echo
$mytheme->getThemeString();


  Files folder image Files (124)  
File Role Description
Files folder imagedoc (1 directory)
Files folder imagesamples (2 files)
Accessible without login Plain text file CSSMagic.geany Data Auxiliary data
Accessible without login Plain text file CSSMagic.php Aux. Auxiliary script
Accessible without login Plain text file Doxyfile Data Auxiliary data
Accessible without login Plain text file LICENCE.pdf Data Auxiliary data
Accessible without login Plain text file LICENCE.txt Lic. License text
Plain text file Rule.php Class Class source
Plain text file Selector.php Class Class source
Plain text file Theme.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:335
This week:0
All time:7,109
This week:488Up