Page 1 of 1
hello I need some help
Posted: Wed Mar 17, 2004 11:47 am
by hemi8785
Yes Im a Newbie
Any who i needed some help with calling a .tpl file that contains html code in it off of my php page.
if you need more explaining on what i mean just ask and ill try my best
Posted: Wed Mar 17, 2004 12:05 pm
by Deemo
i think i know what you are talking about, but if im wrong, tell me
im not 100% sure what a .tpl file is, but there are 2 ways you can read a file that i think you can use
include inserts the code , so you could do like include index.tpl or whatever
require does the same thing as include but gives a fatal error if it can not find the file. include will just skip it
Posted: Wed Mar 17, 2004 12:08 pm
by PrObLeM
template.class.php
Code: Select all
<?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.");
}
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);
}
}
?>
index.php
Code: Select all
<?php
$page = new Page("template.tpl");
//replace tags 'tag'=>"what to replace it with",
$page->replace_tags(array('name' => "timmy", 'email'=>"timmy@test.com"));
$page->output();
?>
Posted: Thu Mar 18, 2004 11:45 am
by hemi8785
What do you mean. Do i create another template.class.php
and put it in my dirctory