Jump to content

[SOLVED] Trying something new with PHP, but it won't work :(


php_phreak_91

Recommended Posts

OK, so I was busy thinking about something new that I could do with PHP as I am always wanting to learn something new that I didn't know before. I figured, wouldn't it be cool to make a template engine, not an advanced one like Smarty, just a simple one, but it won't work :(

 

I can't see anything wrong with the code and PHP is not churning out any errors. Here is the code:

 

template.functions.php:

<?php

class Template { 

var $output; 

function input($page){ 
$this->output = include("templates/default/page_Header.html"); 
$this->output = include("templates/default/page_" . $page . ".html"); 
$this->output = include("templates/default/page_Footer.html"); 
$tags = array(
"SITENAME"	=>		"Template Engine Test Page", 
"PAGETITLE"	=>		"$page"
); 
foreach ($tags as $tag => $value){ 
ereg_replace("{" . $tag . "}", $value, $this->output); 
} 
} 

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

} 

?>

 

index.php

<?php

include("template.functions.php"); 
$template = new Template; 

$template->input("Index"); 
$template->displayPage(); 

?>

 

This should work and replace {SITENAME} and {PAGETITLE} in the html pages, but it isn't, which suggests that I have made an error somewhere in the vicinity of the foreach() section (since that is what deals with the tags). ???

 

Thanks in advance for any help that you may be able to provide.

Nevermind I figured it out, eventually:

 

template.functions.php:

<?php

class Template { 

var $tplpage; 

function input($page){ 
$this->tplpage = join("", file("templates/default/page_" . $page . ".html")); 
$tags = array(
"SITENAME"	=>		"Template Engine Test Page", 
"PAGETITLE"	=>		"$page"
); 
foreach ($tags as $tag => $value){ 
$this->tplpage = eregi_replace("{" . $tag . "}", $value, $this->tplpage); 
} 
} 

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

} 

?>

 

Thanks for your help everyone. :)

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.