0

I followed this tutorial https://css-tricks.com/css-variables-with-php/ to use php variables in CSS. I am using concrete5(version 5.6) CMS. I loaded both .php and .css files.

NewFile.php

<?php
header("Content-type: text/css; charset: UTF-8");

$clRed = 'red';
?>

NewFile.css

 @import "/path/css/NewFile.php?type=cms";

.one-third{
        width: 33%;
        margin: 0 auto;  
        background: <?php echo $clRed;?>;
    }

but css is not reading the php variables.

Chrome Developers tool screenshot

I also referred these links

http://doctype.com/use-php-variables-inside-css-file

how can I use of a php variable into a .css file

PHP Variables in CSS not working

https://www.concrete5.org/community/forums/themes/reusing-editable-values-andor-embedding-php-in-stylesheets

Thanks in advance. Note: I know it's not a recommended practice.

Community
  • 1
  • 1
Neetigya
  • 121
  • 2
  • 10

3 Answers3

2

You forgot that the css IS an PHP.

Look there:

<link rel='stylesheet' type='text/css' href='css/style.php' />

Rename NewFile.css to NewFile.css.php and it will work. But I suggest unificate the files, just call NewFile.php as tutorial says. In the head of php file instantiate:

<?php
header("Content-type: text/css; charset: UTF-8");

$clRed = 'red';
?>

And the rest of the file it's just css with the echos.

capcj
  • 1,535
  • 1
  • 16
  • 23
  • It's not working, what should be the hierarchy. Should I first import *.css.php file or *.php file? – Neetigya May 17 '17 at 20:42
  • Forget the two files, unify them into NewFile.php and put into href of link. – capcj May 17 '17 at 20:46
  • 1
    This works, thank you. For the future readers, this line is very important :- header("Content-type: text/css; charset: UTF-8"); and another good link - http://jafty.com/blog/tag/php-header-content-type-css/ – Neetigya May 18 '17 at 13:38
  • Important to remember: It's a bad practice, but fast implementation. The best case scenario is SASS/LESS or create a CSS generator based into some conf file (can be a PHP file, JSON or INI). – capcj May 18 '17 at 13:55
0

You can't compile php Code in css files. Your linked tutorial will guide you to create a php script that will put out css code and set the header to "text/css". So you are able to link your php file as a css stylesheet into a html page.

In your case you have to echo the content of your css file (without the import) in your php file.

style.php

<?php
header("Content-type: text/css; charset: UTF-8");

$clRed = 'red';

echo '.one-third{
    width: 33%;
    margin: 0 auto;  
    background: '.$clRed.';
}';
?>

index.php

<?php
echo '<link rel="stylesheet" type="text/css" href="style.php" />';
?>
Frankyi
  • 21
  • 3
0

Ok, so first off your error occurs because of the extension you are using is .css not .php. Try this, rename the file extension, then just use the format

<style>
.blabla{
....
}

#heeeHoo{
 ...
}
</style>

as the user hakiko stated in one of your references, then use <?php include 'style.php'; ?> to include it in the desired place. I am fairly certain that PHP include just copies and pastes the contents of a file, so his solution should work. Just make sure to put it in the html <head> tag

Community
  • 1
  • 1
  • Hey, His solution is good but my problem is that my application have 4 sub domains and there will be more subdomains in future, I want to give different color coding to each sub domain, like if a school is logging in the colors should be green and white, when a hospital is using my application then colors should be red and blue. The application already has 15-20 css. It's not possible to change them to .css.php extension so my idea was to create a css.php file and load it after all the the css scripts. In this case using variables will help, I can say if(use=='school') set background = 'red'. – Neetigya May 18 '17 at 12:12
  • You can use `!important` to override other uses of the CSS – Nathaniel Blakely May 19 '17 at 19:14