Seperating CSS from PHP scripts

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Seperating CSS from PHP scripts

Post 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?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Seperating CSS from PHP scripts

Post 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; */
}
 
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: Seperating CSS from PHP scripts

Post 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.
Post Reply