doushi6864 2010-02-10 21:12
浏览 63
已采纳

使用php变量来操纵html标题和正文的Id(是否有人这样做?)

I basically use php variables to store the value of the title and the body's ID. This last one is a technique to have my button (pressed) in the navigation according to which section of the page the user is (in this case the user will know he is currently at "home").

Beginning of my index.php:

<?php
    $title = "New Project";
    $body = "home";
    include("common/header.php");
?>
<div id="content">
    <div class="container">
        <div id="tagline">

Beginning of my header.php

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title><?php echo $title; ?></title>
    <link rel="stylesheet" type="text/css" href="styles/slimbox2.css" />
    <link rel="stylesheet" type="text/css" href="styles/global.css" />
    <link rel="stylesheet" type="text/css" href="styles/home.css" />
    <link rel="stylesheet" type="text/css" href="styles/contact.css" />
    <script type="text/javascript" src="scripts/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="scripts/jquery.corner.js"></script>
    <script type="text/javascript" src="scripts/jquery.validate.js"></script>
    <script type="text/javascript" src="scripts/slimbox2.js"></script>
    <script type="text/javascript" src="scripts/custom.js"></script>
</head>
</head>
<body id="<?php echo $body; ?>">
<div id="header">
    <div class="container">
        <div id="topbar">
            <h1><a href="http://widerdesign.co.nr/">wider design</a></h1>
            <ul id="lang">
                <li><a href="index.php">English</a></li>
                <li><a href="es/index.php">Español</a></li>
                <li><a href="tw/index.php">中文(繁體)</a></li>
                <li><a href="cn/index.php">中文(简体)</a></li>
            </ul>
            <ul id="nav">
                <li class="home"><a href="index.php">home</a></li>
                <li class="products"><a href="products.php">products</a></li>
                <li class="about"><a href="about.php">about</a></li>
                <li class="contact"><a href="contact.php">contact</a></li>
            </ul>

To let the user know in which section he/she is:

#home li.home a, #products li.products a, #contact li.contact {
    color: #999;
}

is there a simpler way of doing this?

Am I using unnecessary those PHP variables?

  • 写回答

4条回答 默认 最新

  • dpir3950 2010-02-10 21:20
    关注

    Yes, what you're doing is fine. I do something similar with a PageTemplate class. In addition to setting the title and navigation, it allows navigation links to appear based on user authentication, allows javascript and css to be added to the head section, etc.

    What you're doing is very common. There are a million ways to do it, but they'll all require setting some variables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?