Jump to content

[SOLVED] Weird bug in my template class?


RecoilUK

Recommended Posts

Hi guys

 

I have a really weird bug in my template class, and I cant work out why it,s happening.

 

<?php

Class TEMPLATE {

  private static $instance = null;  // declare static variable for Singleton pattern.
  private $layout; // container to hold page layout.
  private $page; // container to hold page layout, used during parsing.
  private $moduledir;
  private $rootdir;
  private $boxkey;
  private $boxvalue;
  private $flagkey;
  private $flagvalue;

  protected function __construct() {

    $pos = strrpos($_SERVER['SCRIPT_FILENAME'], '/') + 1;
    $this->moduledir = substr($_SERVER['SCRIPT_FILENAME'], 0,$pos);
    unset($pos);
    $this->rootdir = $_SERVER['DOCUMENT_ROOT'] . '/';
  }

  public static function Instance() {

    if(!self::$instance ) {
      self::$instance = new self(); 
    }
    return self::$instance;
  }

  public function SetLayout($file) {

    if (file_exists($this->moduledir.'templates/'.$file)) {
      $this->layout = file_get_contents($this->moduledir.'templates/'.$file);
    }
    elseif (file_exists($this->rootdir.'templates/'.$file)) {
      $this->layout = file_get_contents($this->rootdir.'templates/'.$file);
    }
    else {
      throw new Exception("Fatal Exception : Layout Template - \"$file\" - Not Found");
    }
    unset($file);
  }

  public function CreateBox($box) {

    $argcount = func_num_args();
    switch($argcount) {
      case 1:
        $box = "<BOX:$box />";
        if (!$this->boxkey[$box]) {
          $this->boxkey[$box] = $box;
          $this->boxvalue[$box] = '';
        }
        break;
      default:
        $argvalues = func_get_args();
        foreach($argvalues as $key => $value) {
          $value = "<BOX:$value />";
          if (!isset($this->boxkey[$key])) {
            $this->boxkey[$value] = $value;
            $this->boxvalue[$value] = '';
          }
        }
    }
    unset ($name);
  }

  public function SetBox($box, $data) {

    $box = "<BOX:$box />";
    if (!isset($this->boxkey[$box])) {
      $this->boxkey[$box] = $box;
    }		
    $this->boxvalue[$box] .= $data;
    unset($box, $data);
  }

  public function SetFlag($var, $value) {

    $this->flagkey[$var] = "<FLAG:$var />";
    $this->flagvalue[$value] = $value;
    unset($var, $value);
  }

  public function ResetBox($box) {

    $this->boxvalue[$box] = '';
    unset ($box);
  }

  public function GetTemplate($file) {

    if (file_exists($this->moduledir . 'templates/' . $file)) {
      return file_get_contents($this->moduledir . 'templates/' . $file);
    } else {
      return file_get_contents($this->rootdir . 'templates/' . $file);
    }
    unset ($file);
  }

  public function ParseResult($box, $file, $result) {

    $html = $this->GetTemplate($file);
    while ($row = $result->fetch_assoc()) {
      foreach ($row as $key => $value) {
        $datakey[$key] = "<DATA:$key />";
        $datavalue[$key] = $value;
      }
      $this->SetBox($box, str_replace($datakey, $datavalue, $html));
      unset ($datakey, $datavalue);
    }
  }

  private function ParsePage() {

    foreach($this->boxkey as $key => $value) {
      if ($this->boxvalue[$key] == '') {
        $this->boxvalue[$key] = $this->boxkey[$value];
      }
    }
    $this->page = str_replace($this->flagkey, $this->flagvalue, 
      str_replace($this->boxkey, $this->boxvalue, $this->layout));
  }

  public function DisplayPage() {

    $this->ParsePage();
    echo $this->page;
  }

}

?>

 

The problem is with $this->layout, whenever it is set, for some weird reason whatever it contains is always preceded by what looks like a newline and two tabs, so whenever you look at the source code, it never starts from the beginning.

 

For example, this is my layout file (layout.html) ...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11-strict.dtd">

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="cmsstyle.css" />
    <title><FLAG:title /></title>
  </head>
  <body>
    <table class="layout" cellspacing="0">
      <tr>
        <td class="header" colspan="2"><BOX:header /></td>
      </tr>
      <tr>
        <td class="menu"><BOX:menu /></td>
        <td class="content"><BOX:content /></td>
      </tr>
      <tr>
        <td class="footer" colspan="2"><BOX:footer /></td>
      </tr>
    </table>
  </body>
</html>

 

Then if you run this page (index.php) ...

 

<?php

if (!@include_once $_SERVER['DOCUMENT_ROOT'].'/classes/template.class.php') {
  print "Fatal Exception : Required Class - \"TEMPLATE\" - Not Found!!";
  exit;
}
if (!@include_once $_SERVER['DOCUMENT_ROOT'].'/classes/database.class.php') {
  print "Fatal Exception : Required Class - \"DATABASE\" - Not Found!!";
  exit;
}
$template = TEMPLATE::Instance();
$db = DATABASE::Instance();

try {
  $template->SetLayout('layout.html');
} 
catch (Exception $e) {
  echo $e->getMessage(), "\n";
  exit;
}
$template->CreateBox('header','menu','content','footer');
$template->SetBox('content', "Hello, and welcome<BR />");
$template->SetFlag('title','Index');



$template->DisplayPage();

?>

 

You finally get this output ...

 

		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11-strict.dtd">

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="cmsstyle.css" />
    <title>Index</title>
  </head>
  <body>
    <table class="layout" cellspacing="0">

      <tr>
        <td class="header" colspan="2"><BOX:header /></td>
      </tr>
      <tr>
        <td class="menu"><BOX:menu /></td>
        <td class="content">Hello, and welcome<BR /></td>
      </tr>
      <tr>

        <td class="footer" colspan="2"><BOX:footer /></td>
      </tr>
    </table>
  </body>
</html>

 

Although, in the output above there looks like only 2 extra tabs, it does infact contain a newline in the source code window.

 

Now its not a huge problem, but I would like to understand where it is coming from.

 

Any ideas?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/113570-solved-weird-bug-in-my-template-class/
Share on other sites

How about characters before the <?php tag in index.php or from anywhere (before any opening <?php tag or after any closing ?> php tag or output by any echo/print statements) in either of your include_once() files?

 

I created index.php, template.class.php and layout.html files from exactly what you posted and commented out the include_once for database.class.php.

 

Content is content. There is nothing operating system or php specific that will cause content to be output or not output, except for perhaps using short open tags <? on systems where that setting is not on or if you have some content or logic in your include files that we have not seen.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.