patternphpModerate
Quick CSS style generator
Viewed 0 times
cssstylequickgenerator
Problem
One day, I was thinking:
Wouldn't it be nice to set a class and have all the styles defined?
And that is exactly what I did: a PHP file that generates CSS with pre-defined styles. So, for fun, I built the following:
`'color',
'force'=>true,
'text'=>true,
'border'=>true,
'back'=>true,
'shadow'=>true,
'sizes'=>true,
'styles'=>true,
'send_header'=>true,
'custom'=>null
);
}
if( isset( $config['send_header'] ) && $config['send_header'] && !headers_sent() )
{
header('Content-type: text/css');
}
$colors = array(
'black',
'red'=>array('dark','indian','mediumviolet','orange','paleviolet'),
'green'=>array('dark','light','forest','yellow','lawn','lime','pale','darkolive','sea','darksea','lightsea','mediumsea','spring','mediumspring'),
'blue'=>array('alice','cadet','cornflower','dark','darkslate','deepsky','dodge','light','lightsky','lightsteel','medium','mediumslate','midnight','powder','royal','sky','slate','steel'),
'white'
);
$sizes=array(
'em'=>array(0.3,0.5,1,1.25,1.3,2,2.5,3,3.3,3.5,4),
'px'=>array(0,1,2,3,4,5,6,7,8,9,10,11,12,12.5,13,14,15,16,17,17.5,18,19,20),
'mm'=>range(0,9),
'cm'=>range(0,9),
'%'=>array(0,1,2,3,4,5,6,7,8,9,10,12.5,15,20,25,30,33.3,35,40,45,50,55,60,62.5,65,66.6,65,70,75,80,85,90,95,99,100)
);
$styles=array(
'border'=>array('none','doted','dashed','solid'),
'text'=>array('none','underline')
);
$important = ( isset($config['force']) && $config['force'] ? '!important' : '' );
$class = ( !isset($config['class']) || $config['class'] === null ? '.color' : ( $config['class'] == false ? '' : '.'.$config['class'] ) );
foreach($colors as $color_name=>$color)
{
if( is_array($color) )
{
foreach( $color as $sub_color )
{
Wouldn't it be nice to set a class and have all the styles defined?
And that is exactly what I did: a PHP file that generates CSS with pre-defined styles. So, for fun, I built the following:
`'color',
'force'=>true,
'text'=>true,
'border'=>true,
'back'=>true,
'shadow'=>true,
'sizes'=>true,
'styles'=>true,
'send_header'=>true,
'custom'=>null
);
}
if( isset( $config['send_header'] ) && $config['send_header'] && !headers_sent() )
{
header('Content-type: text/css');
}
$colors = array(
'black',
'red'=>array('dark','indian','mediumviolet','orange','paleviolet'),
'green'=>array('dark','light','forest','yellow','lawn','lime','pale','darkolive','sea','darksea','lightsea','mediumsea','spring','mediumspring'),
'blue'=>array('alice','cadet','cornflower','dark','darkslate','deepsky','dodge','light','lightsky','lightsteel','medium','mediumslate','midnight','powder','royal','sky','slate','steel'),
'white'
);
$sizes=array(
'em'=>array(0.3,0.5,1,1.25,1.3,2,2.5,3,3.3,3.5,4),
'px'=>array(0,1,2,3,4,5,6,7,8,9,10,11,12,12.5,13,14,15,16,17,17.5,18,19,20),
'mm'=>range(0,9),
'cm'=>range(0,9),
'%'=>array(0,1,2,3,4,5,6,7,8,9,10,12.5,15,20,25,30,33.3,35,40,45,50,55,60,62.5,65,66.6,65,70,75,80,85,90,95,99,100)
);
$styles=array(
'border'=>array('none','doted','dashed','solid'),
'text'=>array('none','underline')
);
$important = ( isset($config['force']) && $config['force'] ? '!important' : '' );
$class = ( !isset($config['class']) || $config['class'] === null ? '.color' : ( $config['class'] == false ? '' : '.'.$config['class'] ) );
foreach($colors as $color_name=>$color)
{
if( is_array($color) )
{
foreach( $color as $sub_color )
{
Solution
As a CSS author, I am horrified by the very concept of this code. There are very goods reasons why we don't use inline styles:
Your application does essentially the same thing, but with classes instead of the style attribute. This goes against over 15 years worth of CSS best practices. There should never be in instance where you have class names that reflects what the element should look like.
You know what this reminds me of? Libraries that programatically generate SQL for you. It looks simple to do at first, then you start realize how many features you have to write interfaces for (recursive queries, CTE, window functions, etc.). At the end of the day, you end up having to write your most complicated queries the old fashioned way, wondering why you're even bothering to use that library in the first place when it only solves your simplest use cases.
- They cannot be reused from one document to the next like an external stylesheet can (ie. they increase page weight if you have more than 1 document)
- They are a maintenance nightmare (oh, you want that doodad to be a different color now? have fun fixing it on every single page)
Your application does essentially the same thing, but with classes instead of the style attribute. This goes against over 15 years worth of CSS best practices. There should never be in instance where you have class names that reflects what the element should look like.
You know what this reminds me of? Libraries that programatically generate SQL for you. It looks simple to do at first, then you start realize how many features you have to write interfaces for (recursive queries, CTE, window functions, etc.). At the end of the day, you end up having to write your most complicated queries the old fashioned way, wondering why you're even bothering to use that library in the first place when it only solves your simplest use cases.
Context
StackExchange Code Review Q#88782, answer score: 19
Revisions (0)
No revisions yet.