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

时间:2016-07-26 13:52:01

标签: php html css

所以我有一些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'>";



?>

3 个答案:

答案 0 :(得分:0)

在这个例子中,我会添加一个函数“$ this-&gt; addCss()”调用一个函数来将你的css作为它需要的链接拉到头部。使用此方法,您可以向addCss()方法添加一个参数,该参数允许您根据某些条件筛选要包含的css表。

答案 1 :(得分:0)

将类中的方法移动到包含似乎很奇怪。现在,当有人想要更新该课程时,他们必须去寻找其他地方。此外,包括你退出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">';
}

答案 2 :(得分:0)

编辑DisplayHeader功能

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