2011-09-07 82 views
0
app dir 



      css 

       style.css 

      inc 

       header.inc.php 

       footer.inc.php 

    index.php 

    login.php 

    register.php 

的style.css包括在的header.inc.php更新包括CSS文件

<?php 

//Include a error reporting: 

include '../../errorReport.inc.php'; 

// Set default timezone: 

define('TZ', date_default_timezone_set('America/Los_Angeles')); 

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 

<head> 

    <title> 

    <?php 

    if(defined('TITLE')) 

    { 

     print TITLE; 

    } else { 

     print 'Raise High the Roof Beam! A J.D. Salinger Fan Club'; 

    } 



    ?> 

    </title> 

    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 

    <link rel="stylesheet" href="./c/screen.css" media="screen,projection" /> 

</head> 

在創建一個新的頁面(login.php中)的目標是將樣式應用到包括( d)標題中的CSS。不是簡單地把CSS的標記,在任何地方,下面是我的嘗試,它已經失敗:

<?php 

header('content-type:text/css', replace); 

header("Expires: ".gmdate("D, d M Y H:i:s", (time()+900)) . " GMT"); 

$errorColor = '#900'; 

echo '.error{ color: '.$errorColor.'; }'; 

?> 

<?php include 'inc/header.inc.php';?> 

<p class=\"error\">Error MSG</p> 

<?php include 'inc/footer.inc.php';?> 

這是打印到UA:

.error{ color: #900; } 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head> 
    <title> 
    Raise High the Roof Beam! A J.D. Salinger Fan Club </title> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
    <link rel="stylesheet" href="./c/screen.css" media="screen,projection" /> 
</head> 
<body> 
<div id="wrapper"> 
................................. 
+1

如果你輸出文本/ CSS MIME類型,那麼整個文件應該是CSS。在某些HTML中添加只會導致無效/破壞的CSS。 –

回答

0

.error{ color: #900; }需求是內<style>標記在<head>之內,不在文檔的最頂端。

<?php 
    header('content-type:text/css', replace); 
    header("Expires: " . gmdate("D, d M Y H:i:s", (time()+900)) . " GMT"); 

    $errorColor = '#900'; 
    $css  = array(); 
    $css[]  = '.error{ color: '.$errorColor.'; }'; 
?> 

<?php include 'inc/header.inc.php';?> 
<p class="error">Error MSG</p> 
<?php include 'inc/footer.inc.php';?> 

然後,裏面inc/header.inc.php,有你<head>標籤內是這樣的:

if (is_array($css)) { 
    echo '<style type="text/css">' . implode("\n", $css) . '</style>'; 
} 
+0

謝謝,我從目標文件中刪除了頭文件(),並且css數組()按需要工作。另外,看看用PHP打開和重寫css文件。非常感謝。 – Wasabi

1

簡單的答案是,你應該包括這個CSS的樣式表,style.css的,因爲它是不變。

如果你希望這是動態的,你需要修改你的方案。這是一個框架將會有所幫助的地方,因爲你做出的任何選擇都會有些破綻。話雖這麼說,考慮這些選項:

A)在login.php中,設置$headerCss = '.error { color: '.$errorColor.'; } ',然後在文件header.inc.php,在</head> tag之前,添加:

if ($headerCss) { 
    print '<style type="text/css">'; 
    print $headerCss; 
    print '</style>'; 
} 

B)另一種可能的黑客是設置$pageName = 'login',然後有在的header.inc.php頁面名稱測試開關,如:

if ($pageName == 'login') { 
    print '<style type="text/css">'; 
    print ' .error { color: #900; }'; 
    print '</style>'; 
} 
0

更新以下內容,INC /的header.inc.php:

<?php 
//Include a error reporting: 
include '../../errorReport.inc.php'; 
// Set default timezone: 
define('TZ', date_default_timezone_set('America/Los_Angeles')); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head> 
    <title> 
    <?php 
    if(defined('TITLE')) 
    { 
     print TITLE; 
    } else { 
     print 'Raise High the Roof Beam! A J.D. Salinger Fan Club'; 
    } 

    ?> 
    </title> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
    <link rel="stylesheet" href="./c/screen.css" media="screen,projection" /> 

    <?php 
    if(is_array($css)) { 
    echo '<style type="text/css">'; 

    foreach ($css as $constant) 
    { 
     //print"<p />$prop"; 
      foreach($constant as $vars){ 

      echo $vars; 

      } 
    } 
    echo '</style>'; 
} 
    ?> 
</head> 
<body> 
<div id="wrapper"> 

    <div id="header"> 
     <p class="description">A J.D. Salinger Fan Club</p> 
     <h1><a href="index.php">Raise High the Roof Beam!</a></h1> 
     <ul id="nav"> 
      <li><a href="books.php">Books</a></li> 
      <li><a href="#">Stories</a></li> 
      <li><a href="#">Quotes</a></li> 
      <li><a href="login.php">Login</a></li> 
      <li><a href="register.php">Register</a></li> 
     </ul> 
    </div><!-- header --> 

    <div id="sidebar"> 
     <h2>Favorite Quotes</h2> 
      <p class="news">I don't exactly know what I mean by that, but I mean it.<br />- <em>The Catcher in the Rye</em></p> 
      <p class="news">I privately say to you, old friend... please accept from me this unpretentious bouquet of early-blooming parentheses: (((()))).<br />- <em>Raise High the Roof Beam, Carpenters and Seymour: An Introduction</em></p> 
    </div><!-- sidebar --> 

    <div id="content"> 
     <!-- BEGIN CHANGEABLE CONTENT. --> 

CSS測試:

<?php 
$errorColor = "#900"; 
$fontSize = "5em"; 


$cssColors = array('.error{ color: '.$errorColor.';}'); 
$cssfontSize = array('#mainHead{font-size: ' . $fontSize.';}'); 
$css = array('colors' => $cssColors, 'fontSize' => $cssfontSize); 

?> 

<?php include 'inc/header.inc.php';?> 
<p class="error">Error MSG</p> 
<?php include 'inc/footer.inc.php';?>