SunQuest
           Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today!
  #1  
Old October 4th, 2004, 10:50 PM
spGhe spGhe is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Kissimmee, Florida, US
Posts: 130 spGhe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
Writing a Template System in PHP - Help Please

I was a little confused when reading the tutorial because I am new to the whole templating thing. Anyway, below I will have the error and code. Thanks in advance to anyone who helps or tries to help!

--- Error ---

Fatal error: Cannot instantiate non-existent class: page in /home/whomikey/public_html/template system/index.tpl.php on line 4

--- index.tpl.php/template.tpl ---

<?
require_once('lib/template.php');

$page = new Page('std.tpl');

$page->replace_tags(array(
'title' => 'title',
'user_bar' => 'dat/user_bar.dat',
'navigation' => 'dat/navigation.dat',
'main' => 'dat/index.dat',
'affiliates' => 'dat/affiliates.dat',));

$page->output();
?>

Reply With Quote
  #2  
Old October 5th, 2004, 12:02 AM
nawlej nawlej is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Dallas, Tx. USA
Posts: 2,008 nawlej User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 7 m 51 sec
Reputation Power: 4
RE: Writing a Template System in PHP - Help Please

it almost looks like its not finding the class functions.

Reply With Quote
  #3  
Old October 5th, 2004, 12:09 AM
spGhe spGhe is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Kissimmee, Florida, US
Posts: 130 spGhe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Writing a Template System in PHP - Help Please

I have the code for std.tpl which has all the functions if you need to see that. I will post it below to save time. It's the same as the one that was in the tutorial because I didn't want to change around or add anything untill I get it working from the way it was originally.

--- std.tpl ---

<?
class Page
{
var $page;

function Page($template = 'std.tpl') {
if (file_exists($template))
$this->page = join('', file($template));
else
die("Template file $template not found.");
}

function parse($file) {
ob_start();
include($file);
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}

function replace_tags($tags = array()) {
if (sizeof($tags) > 0)
foreach ($tags as $tag => $data) {
$data = (file_exists($data)) ? $this->parse($data) :
$data;
$this->page = eregi_replace('{' . $tag . '}', $data,
$this->page);
}
else
die('No tags designated for replacement.');
}

function output() {
print($this->page);
}
}
?>

Reply With Quote
  #4  
Old October 5th, 2004, 12:16 AM
nawlej nawlej is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Dallas, Tx. USA
Posts: 2,008 nawlej User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 7 m 51 sec
Reputation Power: 4
RE: Writing a Template System in PHP - Help Please

The tpl files are for your html templates. You need to put those functions in a php file and require_once that php file you put them in.

Reply With Quote
  #5  
Old October 5th, 2004, 12:21 AM
spGhe spGhe is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Kissimmee, Florida, US
Posts: 130 spGhe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Writing a Template System in PHP - Help Please

I am a little confused with what you just said, lol. Would that mean std.tpl = std.php and index.tpl.php = index.php? If you can explain it a little more to me. Thanks!

Reply With Quote
  #6  
Old October 5th, 2004, 12:31 AM
nawlej nawlej is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Dallas, Tx. USA
Posts: 2,008 nawlej User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 7 m 51 sec
Reputation Power: 4
RE: Writing a Template System in PHP - Help Please

put the Page class in a separate file. Call it template.php

Now, use this code:

php Code:
Original - php Code
  1.  
  2. <?
  3. require_once('template.php');
  4.  
  5. $page = new Page('std.tpl');
  6.  
  7. $page->replace_tags(array(
  8. 'title' => 'title',
  9. 'user_bar' => 'dat/user_bar.dat',
  10. 'navigation' => 'dat/navigation.dat',
  11. 'main' => 'dat/index.dat',
  12. 'affiliates' => 'dat/affiliates.dat',));
  13.  
  14. $page->output();
  15. ?>


That will take std.tpl, and replace all of the tags in it that you want it to from above, and your Page class resides in template.php

Reply With Quote
  #7  
Old October 5th, 2004, 12:48 AM
spGhe spGhe is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Kissimmee, Florida, US
Posts: 130 spGhe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Writing a Template System in PHP - Help Please

Hehe, I tried to do it but I know I did it wrong because I am still confused and there was an error. When you get time it would be cool if you could checkout my code I will post it for each page below. Thanks for the help so far!

--- template.php ---

<?
class Page
{
var $page;

function Page($template = 'std.tpl') {
if (file_exists($template))
$this->page = join('', file($template));
else
die("Template file $template not found.");
}
}
?>

--- template.tpl ---

<?
require_once('template.php');

$page = new Page('std.tpl');

$page->replace_tags(array(
'title' => 'title',
'user_bar' => 'dat/user_bar.dat',
'navigation' => 'dat/navigation.dat',
'main' => 'dat/index.dat',
'affiliates' => 'dat/affiliates.dat',));

$page->output();
?>

--- std.tpl ---

<?
function parse($file) {
ob_start();
include($file);
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}

function replace_tags($tags = array()) {
if (sizeof($tags) > 0)
foreach ($tags as $tag => $data) {
$data = (file_exists($data)) ? $this->parse($data) :
$data;
$this->page = eregi_replace('{' . $tag . '}', $data,
$this->page);
}
else
die('No tags designated for replacement.');
}

function output() {
print($this->page);
}
?>


Reply With Quote
  #8  
Old October 5th, 2004, 01:20 AM
nawlej nawlej is offline
Contributing User
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Dallas, Tx. USA
Posts: 2,008 nawlej User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 7 m 51 sec
Reputation Power: 4
RE: Writing a Template System in PHP - Help Please

*doh* lol....

The idea of the object oriented style is for the templating functions to be in the same class.....

template.php:
php Code:
Original - php Code
  1.  
  2. <?
  3. class Page{
  4.  var $page;
  5.  
  6.  function Page($template = 'std.tpl') {
  7.   if (file_exists($template)){
  8.      $this->page = join('', file($template));
  9.   }else{
  10.      die("Template file $template not found.");
  11.   }
  12.  }
  13.  
  14.  function parse($file) {
  15.   ob_start();
  16.   include($file);
  17.   $buffer = ob_get_contents();
  18.   return $buffer;
  19.  }
  20.  
  21.  function replace_tags($tags = array()) {
  22.   if (sizeof($tags) > 0)
  23.    foreach ($tags as $tag => $data) {
  24.     $data = (file_exists($data)) ? $this->parse($data) :
  25.     $data;
  26.     $this->page = eregi_replace('{' . $tag . '}', $data,
  27.     $this->page);
  28.    }
  29.   }else{
  30.    die('No tags designated for replacement.');
  31.   }
  32.  }
  33.  
  34.  function output() {
  35.   print($this->page);
  36.  }
  37.  
  38. }

Reply With Quote
  #9  
Old October 5th, 2004, 08:00 AM
spGhe spGhe is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Kissimmee, Florida, US
Posts: 130 spGhe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 2
RE: Writing a Template System in PHP - Help Please

I got it working finally! Thanks a lot for helping me.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOtherTutorials > Writing a Template System in PHP - Help Please


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by