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

使用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?

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.

This works, but you should consider using a template engine such as Smarty instead.

Or else use an MVC framework like CakePHP

janoChen,

I use the following method:

// get the url

$url= $_SERVER['PHP_SELF'];

// add a class on the menu item

<ul id="lang">
      <li><a href="index.php" <?php if (strpos($url, "index.php")) { echo " class='active'"; } ?>>English</a></li>

//with css I set a style for the class active

li a.active { 
 color: #990000;
}

But you method also works.