Jump to content

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. :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.