Page 1 of 1

Seperating CSS from PHP scripts

Posted: Wed Mar 05, 2008 5:40 am
by LonelyProgrammer
One of the undying problems I have faced when coding PHP pages are CSS styles embedded into HTML, which is either echoed out - or returned as string(I usually separate pages into components, and each component will just return the html code that 'renders' it).

I have tried templating (Smarty in particular) and it works fine if you have static component placement and you have a tpl for each page, but I am using a single page system. So it means I need to have a tpl which in the end usually end up looking like this <div style="blah">{$placeholder}</div> just for that particular style. And for each different component, I will need a seperate tpl because there is no saying how many components a page will attach to itself.

Is there any solution to this? Can PHP variables be include in CSS stylesheets somehow so that there will be an automatic sync?

Re: Seperating CSS from PHP scripts

Posted: Wed Mar 05, 2008 8:00 am
by Zoxive

Code: Select all

<?php
 
header("Content-type: text/css");
ob_start("ob_gzhandler");
 
$BodyFontColor = '#ffebcd';
$BodyFont = 'Tahoma,Genva,sans-serif';
$Title = '#e7e495';
$ImgUrl = '/cache/imgs/';
?>
/* Tag Specifics */
html,body{
  height:100%;
  font-family:<?php echo $BodyFont; ?>;
  font-size:0.9em;
}
body{
  background-color:#001115;
  color:<?php echo $BodyFontColor; ?>;
}
p{
  padding:0;
  margin:0;
  margin-top:8px;
  padding-bottom:8px;
  /* text-indent:35px; */
}
 

Re: Seperating CSS from PHP scripts

Posted: Wed Mar 05, 2008 4:47 pm
by Sekka
I won't comment on the CSS problem as from what I can tell Zoxive has answered it very well.

One thing I will say is that you should easily be able to implement what you need with Smarty. I make very complicated content managed websites and Smarty has more than coped with my needs.

If you were having problems, I suggest you find some good tutorials on how to use Smarty to fit your needs.