hello I need some help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hemi8785
Forum Newbie
Posts: 2
Joined: Wed Mar 17, 2004 11:47 am

hello I need some help

Post 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
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post 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
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post 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();
?>
hemi8785
Forum Newbie
Posts: 2
Joined: Wed Mar 17, 2004 11:47 am

Post by hemi8785 »

What do you mean. Do i create another template.class.php
and put it in my dirctory
Post Reply