views:

370

answers:

3

Ok so i have a template script my friend built for me. I'll include all file names.

OK so what is not working is file_get_contents is not grabing the content

(1 I don't know where the content should be placed.

(2 I want it placed in a directory so that IF i change the template the area where content stays is the same.

(3 I'm trying to get file_get_contents to load the links ?=about ?=services etc into to body.tpl in the contents div i have specified with #CONTENTS#

(4 The Dir Tree is as follows

 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)

please any help is appreciated. whats the issues is now the script is putting everything in the correct area loading it just not displaying it?.

/* 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'])){
    // if not, lets load our index page(you can change home.php to whatever you want:
    ob_start();
    include("contents/".'main.php');
    $content = ob_get_contents();
    ob_end_clean();
    // else $_GET['page'] was set so lets do stuff:
    } else {
    // lets first check if the file exists:
    if(file_exists($_GET['page'].'.php')){
    // and lets include that then:
    ob_start();
    include("contents/". $_GET['page'] . '.php');
    $content = ob_get_contents();
    ob_end_clean();


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

if some one could trim that down so it JUST is the template required code and file get contents.

/* index.php */

<?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 */

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd"&gt;
<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"&gt;&lt;/script&gt;
</head>
<body>
 <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>
+1  A: 

Hello s32ialx,

the template script that your friend built for you is filled with helper functions that will help you with templating your site.

currently, I think you have 2 problems :

  1. the location of the templates
  2. using the template mechanism

1. the location of the templates

Currently, your friend's script states

var $path = "templates/clean";

which means that you should create a templates folder and a templates/clean folder in the directory where you have put index.php and put your templates (header.tpl, body.tpl, footer.tpl) inside.

2. using the template mechanism

Try using

<?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 "Hello World!"
    $body = $file->setcontent($body, "Hello World!");

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

?>

The setcontent find the occurence of #CONTENT# in the pre-loaded body.tpl and replaces it with a specific content (in this case, "Hello World")

I hope this will put you on good track ! tell me if it works. Jerome

Jerome WAGNER
ok SO what did was added "$body = $file->setcontent($body, $content);" like you said but changed "Hello World!" to "$content" so it reads that tag and it works it goes to read using"if (isset($_GET['page'])) { $content = $_GET['page'].'.php'; } else { $content = 'main.php';}"so when i click about and the script reads <a href="page?=about" it loads the content into the correct div in the body.tpl BUT it just shows about.php not the actual content any thoughts?
s32ialx
+2  A: 

Hey,

From what I understand, your content is inside about.php (for instance).

this file is in the "contents" directory : contents/about.php

the command

include('about.php')

will load and execute the script about.php inline. So it will print its content exactly where the include call is done. What you see must look like :

(content of about.php) | header | body | footer

If you want $content to have the correct value, change the line

include($_GET['page'].'.php');

with

ob_start();
include("contents/". $_GET['page'] . '.php');
$content = ob_get_contents();
ob_end_clean();

this will use the object buffering technique to make PHP understand that it should not render the content of the file inline, but put it in the $content variable instead.

I Hope this helps !

Jerome WAGNER
I tried doing what you said and it doesn't change a thing. i tried both updated it in op
s32ialx
I modified the snipped to take into account the fact that "about.php" is in "contents/about.php". Tell me if it work.Please do not keep the "exploded" version ob_start/ob_end_clean. you need to haveob_start();INCLUDE(.....)$content = ob_get_contents();ob_end_clean();around each include. I'll clarify if this is not clear.If this does not work, can you elaborate on 1/ what is the URL that you are calling in your browser ?2/ what you are seeing ?Jerome
Jerome WAGNER
genius! thanks alot that worked great the contents is where it should be now (found out because it was looking for contents/main.php and it was not there so it showed not found jargon until i put it in the folder to bad a new issue appears to risen where the content doesn't display though. I've updated my code to show what i have done
s32ialx
the site if you would like to see is http://designed.sytes.net/index.php this is just the development address
s32ialx
i've made a .phps for both index.php and file.class.php
s32ialx
A: 

Hey

I think your problem may be on the line

if(file_exists($_GET['page'].'.php')){

It should read

if(file_exists('contents/'.$_GET['page'].'.php')){

Hope this helps

Jerome WAGNER