将CSS添加到php文件的最佳方式


Best way to add CSS to php files?

所以我有一些php代码,它使用require函数来帮助我快速制作页面。所以主.inc文件就在这里。正如你所看到的,有一些html可以添加颜色和类似的东西。我想现在使用样式表添加css。我会在这个文件或其他文件的顶部添加样式表语法(.css)吗?

<?php
class Page
{
  // class Page's attributes
  public $content;
  public $title = 'TLA sdfds Pty Ltd';
  public $keywords = 'TLA Consulting, Three Letter Abbreviation, 
                   some of my best friends are search engines';
  public $buttons = array( 'Home'     => 'home.php', 
                        'Daily Specials'  => 'daily_special.php', 
                        'Car Wash' => 'car_wash.php', 
                        'Gas Station ' => 'gas_station.php',
                        'Deli ' => 'deli.php',
                      );
  // class Page's operations
  public function __set($name, $value)
  {
    $this->$name = $value;
  }
  public function Display()
  {
    echo "<html>'n<head>'n";
    $this -> DisplayTitle();
    $this -> DisplayKeywords();
    $this -> DisplayStyles();
    echo "</head>'n<body>'n";
    $this -> DisplayHeader();
    $this -> DisplayMenu($this->buttons);
    echo $this->content;
    $this -> DisplayFooter();
    echo "</body>'n</html>'n";
  }
  public function DisplayTitle()
  {
    echo '<title> '.$this->title.' </title>';
  }
  public function DisplayKeywords()
  {
    echo "<meta name='"keywords'" content='"$this->keywords'" />";
  }
  public function DisplayStyles(){
      echo '<link rel="stylesheet" type="text/css" href="path/to/app.css">';
  }
  public function DisplayHeader(){

}
  public function DisplayMenu($buttons)
  {
    echo "<table width='100%' bgcolor='white' cellpadding='4' 
                cellspacing='4''n";
    echo "  <tr>'n";
    //calculate button size
    $width = 100/count($buttons);
    while (list($name, $url) = each($buttons))
    {
      $this -> DisplayButton($width, $name, $url, !$this->IsURLCurrentPage($url));
    }
    echo "  </tr>'n";
    echo "</table>'n";
  }
  public function IsURLCurrentPage($url)
  {
    if(strpos($_SERVER['PHP_SELF'], $url )==false)
    {
      return false;
    }
    else
    {
      return true;
    }
  }
  public function DisplayButton($width, $name, $url, $active = true){
    if ($active)
    {
      echo "<td width ='$width%'>
            <a href ='$url'>
            <a href ='$url'><span class='menu'>$name</span></a></td>";
    }  
    else
    {
      echo "<td width ='$width%'>
      <!-- this is where the side arrow goes on home page -->
            <img src ='side-logo.gif'>
            <span class='menu'>$name</span></td>";
    }  
  }
  public function DisplayFooter(){
?>
    <table width = "100%" bgcolor ="white" cellpadding ="12" border ="0">
    <tr>
      <td>
        <p class="foot">&copy; TLA Consulting Pty Ltd.</p>
        <p class="foot">Please see our 
                      <a href ="">legal information page</a></p>
      </td>
    </tr>
    </table>
<?php   
  }
}

//css文件

h1 {
    color:red; font-size:24pt; text-align:center; 
    font-family:arial,sans-serif
}
.menu {
    color:red; font-size:12pt; text-align:center; 
    font-family:arial,sans-serif; font-weight:bold
}

example of using require function:


<?php
require('page.inc');
class ServicesPage extends Page{
private $row2buttons =  array ('Re-engineering' => 'reengineering.php', 'Standards Compliance' => 'standards.php');

    public function Display(){
    echo "<html>'n<head>'n";
    $this -> DisplayTitle();
    $this -> DisplayKeywords();
    $this -> DisplayStyles();
    echo "</head>'n<body>'n";
    $this -> DisplayHeader();
    $this -> DisplayMenu($this->buttons);
    #$this -> DisplayMenu($this->row2buttons); #this line calls display menu a second time and creates a second menyu bar
    echo $this->content;
    $this -> DisplayFooter();
    echo "</body>'n</html>'n";
  }//end Display
} //end class page


    $services = new ServicesPage(); //create object
    $services-> content = '<p>This is the new page and it goes here </p>';
    $services->Display();
 echo "<body style='background-color:#F7F7F7'>";

?>

将方法从类移到include似乎很奇怪。现在,当有人想更新该类时,他们必须去寻找其他地方。此外,在该include中,您退出php?>无疑会导致一些错误。

你所做的让页面不那么杂乱是个好主意,但更好的方法可能是删除样式标签并引入样式表。

所以在app.css中,您会有:

h1 {
    color:white; font-size:24pt; text-align:center; 
    font-family:arial,sans-serif
}
.menu {
    color:white; font-size:12pt; text-align:center; 
    font-family:arial,sans-serif; font-weight:bold
}
/*ALL THE STYLES HERE*/

有了这个,你现在所要做的就是

public function DisplayStyles() { 
    echo '<link rel="stylesheet" type="text/css" href="path/to/app.css">';
}

在这个例子中,我会添加一个函数"$this->addCss()",该函数调用一个函数,将css拉到头中作为所需的链接。使用此方法,您可以在addCs()方法中添加一个参数,该参数允许您根据一些条件筛选要包含的css表。

编辑DisplayHeader函数

public function DisplayHeader(){
   echo '<link rel="stylesheet" type="text/css" href="your css path">';
}