Including Stylesheets From PHP

From Facebook Developer Wiki

Jump to: navigation, search

Including Stylesheets From PHP

As FBML does not allow the usage of the link tag and other external references (now in beta!), you will have to include your CSS rules in the output.

Example 1:

 <style>
 <?php require("style.css"); ?>
 </style>

Example 1 is simple, but not recommended - it may cause your page not to validate as XML if the CSS rules have any '<' characters in them, and if there's anything that happens to look like PHP code in your stylesheet then it will probably also cause your page to crash.

Example 2:

 <style>
 <?php 
   ob_start();
   require("style.css");
   $buffer = ob_get_contents();
   ob_end_clean();
   echo htmlspecialchars($buffer);
 ?>
 </style>

Example 2 fixes the XML problem in example one, but it's cumbersome to remember, and therefore also not recommended. It also doesn't fix the invalid PHP problem.

Example 3:

 <style>
 <?php echo htmlentities(file_get_contents('style.css', true)); ?>
 </style>

Example 3 is clearly the winner: it fixes both problems, and as a bonus is still quite easy to read and remember.

The 'true' parameter means 'search in the include path', and is not required if the stylesheet is in the working directory (which is usually the same directory as the script).

reference