在标头中更新包含的CSS文件

时间:2011-09-07 18:00:04

标签: php css

app dir



          css

              style.css

          inc

              header.inc.php

              footer.inc.php

  index.php

  login.php

  register.php

style.css包含在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" />

</head>

创建新页面(login.php)后,目标是在标题中的include(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">
.................................

3 个答案:

答案 0 :(得分:1)

简单的答案是,您应该在样式表style.css中包含此CSS,因为它是常量。

如果您希望这是动态的,则需要稍微修改一下您的方案。这是一个框架将有用的地方,因为你做出的任何选择都将是一个黑客攻击。话虽如此,请考虑以下选项:

A)在login.php中设置$headerCss = '.error { color: '.$errorColor.'; } ',然后在header.inc.php中,在</head>标记之前添加:

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>';
}

答案 1 :(得分: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>';
}

答案 2 :(得分: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 TEST:

<?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';?>