$ _GET在head标记之前加载内容而不是在指定的div中加载内容

时间:2010-03-31 07:23:56

标签: php css xhtml html

问题在于,当我在index.php文件中添加<!DOCTYPE>, <html> <head></head>之前加载内容时,它会在标签之前显示NOW $ content在Body标签之前加载...当它应该加载到“内容”之前“div。

(1我正在尝试让file_get_contents加载链接?= about?= services等到我用#CONTENTS#

指定的内容div中的body.tpl

(2 Dir Tree如下

 htdocs>
    classes> file.class.php
    contents> where i want #CONTENTS# (file_get_contents) to grab data from
    images> content (changing images)
    templates> where the templates are hosted
            clean>main template (Files are header.tpl, body.tpl, footer.tpl, styles.css, menu_style.css, and the images folder for images relating to the template itself.)
            other templates>(to come)

/ * file.class.php * /

<?php

$file = new file();

class file{
    var $path = "templates/clean";
    var $ext = "tpl";

    function loadfile($filename){
  return file_get_contents($this->path . "/" . $filename . "." . $this->ext);
 }

 function setcontent($content,$newcontent,$vartoreplace='#CONTENT#'){
  $val = str_replace($vartoreplace,$newcontent,$content);
  return $val;
 }

 function p($content) {
  $v = $content;
  $v = str_replace('#CONTENT#','',$v);
  print $v;
 }
}
if(!isset($_GET['page'])){
    ob_start();
    // if not, lets load our index page(you can change home.php to whatever you want:
    include("main.txt");
    // else $_GET['page'] was set so lets do stuff:
    } else {
    // lets first check if the file exists:
    if(file_exists($_GET['page'].'.txt')){
    // and lets include that then:
    include($_GET['page'].'.txt');
    $content = ob_get_contents();
    ob_end_clean();

    // sorry mate, could not find it:
    } else {
        echo 'Sorry, could not find <strong>' . $_GET['page'] .'.txt</strong>';
    }
}
?>

如果有人可以减少它,那么JUST是模板所需的代码和文件获取内容。

/ * index.php * /

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<meta name="robots" content="index,follow"/>
<meta name="distribution" content="global"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<link href="templates/clean/style.css" rel="stylesheet" type="text/css" media="screen" />
<link rel="stylesheet" href="templates/clean/menu_style.css" type="text/css" />
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
</head>
<body> //everything before this USED to be in header.tpl but i moved it here until fix so that w3c validator would validate my code
<?php
    include('classes/file.class.php');

    // load the templates
    $header = $file->loadfile('header');
    $body = $file->loadfile('body');
    $footer = $file->loadfile('footer');

    // fill body.tpl #CONTENT# slot with $content
    $body = $file->setcontent($body, $content);

    // cleanup and output the full page
    $file->p($header . $body . $footer);

?>

/ * header.tpl * /

 <div id="header">
  <div id="logo"><a href="index.php" style="height:30px;width:150px;"><img src="images/logo.png" border="0" alt=""/></a></div>
   <div id="menuo"> 
    <div class="menu">
     <ul id="menu">
      <li><a href="?page=home">Home</a></li>
      <li><a href="?page=about">About Us</a></li>
      <li><a href="?page=services">Services</a>
       <ul>
        <li><a href="?page=instore">InStore Repairs</a></li>
        <li><a href="?page=inhome">InHome Repairs</a></li>
        <li><a href="?page=website">Website Design</a></li>
        <li><a href="?page=soon">Comming Soon.</a></li>
         </ul>
       </li>
      <li><a href="?page=products">Products</a>
                   <ul>
                   <li><a href="?page=pchard">Computer Hardware</a></li>
                   <li><a href="?page=monitor">Monitor's</a></li>
                   <li><a href="?page=laptop">Laptop + Netbooks</a></li>
                   <li><a href="?page=soon">Comming Soon.</a></li>
                   </ul>
             </li>
      <li><a href="?page=contact">Contact</a></li>
     </ul>
    </div>
   </div>
 </div>
 <div id="headerf">
 </div>

/ * body.tpl * /

   <div id="bodys">
    <div id="bodt"></div>
        <div id="bodm">
            <div id="contents">
                #CONTENT#
                </div>
    <div id="bodb"></div>
        </div>
</div>

/ * footer.tpl * /

<div id="footer">
<div style="position:absolute; top:4px; left:4px;"><img src="images/ff.png" alt="ok"></div> <div style="position:absolute; top:4px; right:5px; color:#FFFFFF;">&copy;2010 <a href="mailto:">Company Name</a></div>
</div>
</body>
</html>

My Firefox Showing The Problem In Action

3 个答案:

答案 0 :(得分:0)

编辑回答: 我原先假设p()方法是你正在调用的偶然方法,但是出现(根据你为确定两个页面相同的事实所做的更新)是一个红色的鲱鱼。但问题可能类似。

当你: include('classes/file.class.php');作为index.php的一部分,它所做的一件事就是定义方法,然后运行一些if语句,作为此更改的一部分,它包含一个.txt文件。 (例如include("main.txt");)。

我猜你所看到的是这个包含文件显示在屏幕上的内容。它们被包括在内有什么意义?你能把它们删除吗?您是否真的只是将内容读取到变量而不是包含它(并有效地回显/打印到屏幕上)?

尝试删除它们并查看是否能解决您的问题 - 但是,请检查以确保您的模板文件包含您认为它们包含的内容。 :)

答案 1 :(得分:0)

我有点困惑,因为你的index.php代码与你的file.class.php代码相同(这是一个错误?)。但如果我不得不猜测你的问题可能出现在$ file-&gt; p()函数的打印行中:

function p($content) {
  $v = $content;
  $v = str_replace('#CONTENT#','',$v);
  print $v;
 }

此功能很好,具体取决于您何时调用它。如果您尚未向屏幕输出任何内容,并且调用p()并将其传递给body.tpl的内容,则可能会导致您输出的HTML输出不正确。

答案 2 :(得分:0)

获取firebug并检查应用于内容div和文本的样式。这似乎是一个position问题。