Code Snippet: A Simple Templating Class - preCharge Forums
It shows that you are unregistered. Please register with us by clicking Here
preCharge Forums


Nav Green LeftNav Right
preCharge Forums > Website Design & Development > Programming > PHP » Code Snippet: A Simple Templating Class


View Poll Results: Cast your vote for your favorite:
1. Brandi 1 11.11%
2. the other blonde 3 33.33%
3. Brunette in the cowboy hat 1 11.11%
4. Meghan 4 44.44%
5. Actress/Singer 0 0%
6. Girl in Grey Shirt 0 0%
Voters: 9. You may not vote on this poll

Reply
Tcat Right
 
LinkBack Thread Tools Display Modes Tcat Right
Old 07-22-2006   #1 (permalink)
Jamie
Senior Member
 
Jamie's Avatar
 
Join Date: Jul 2006
Location: Swansea, Wales, UK
Age: 20
Posts: 239
Default Code Snippet: A Simple Templating Class

Code:
<?php

class IdevsPage
{

var $Template;
var $Replacement;

function IdevsPage($template="")
{
	if(file_exists($template)) $this->Template = file($template);
}


function BuildReplacement($start,$finish)
{
	$count = count($this->Replacement);
 	$this->Replacement[$count][0] = $start;
 	$this->Replacement[$count][1] = $finish;
}



function PopulatePage()
{
	for($x=0;$x<=count($this->Template)-1;$x++)
	{
		$NewArray[$x] = $this->Template[$x];

		for($y=0;$y<=count($this->Replacement)-1;$y++)
		{
			$NewArray[$x] = str_replace($this->Replacement[$y][0],$this->Replacement[$y][1],$NewArray[$x]);
		}
	}
$this->Template = $NewArray;
return TRUE;
}

function LoadPage()
{
	for($x=0;$x<=count($this->Template)-1;$x++)
	{
		echo $this->Template[$x];
	}
return TRUE;
}

}

class IdevsModule
{

var $Template;
var $Replacement;

function IdevsModule($template="")
{
	if(file_exists($template)) $this->Template = file($template);
}


function BuildReplacement($start,$finish)
{
	$count = count($this->Replacement);
 	$this->Replacement[$count][0] = $start;
 	$this->Replacement[$count][1] = $finish;
}



function MakeModule()
{
	$NewArray = array();
	for($x=0;$x<=count($this->Template)-1;$x++)
	{
		$NewArray[$x] = $this->Template[$x];

		for($y=0;$y<=count($this->Replacement)-1;$y++)
		{
			$NewArray[$x] = str_replace($this->Replacement[$y][0],$this->Replacement[$y][1],$NewArray[$x]);
		}
	}
$this->Template = $NewArray;
return TRUE;
}

function GetModule()
{
	$ReturnValue = "";
		for($x=0;$x<=count($this->Template)-1;$x++)
		{
			$ReturnValue .= $this->Template[$x];
		}
return $ReturnValue;
}

}

?>
OK...firstly sorry about the non-commented code ive been meaning to do it for ages but things keep poping up it is pretty self explanitory for thoughs with some php knowledge.

Now for some sample code:

index.itf
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="english">
<head>
<title><!Title!></title>
<meta name="author" content="Jamie Lewis" />
<meta name="description" content="The development blog and portfolio of Jamie Lewis." />
<meta name="keywords" content="idevs, web, dev, development, internet, portfolio, jamie, lewis" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="styles/main.css"/>
<link rel="shortcut icon" href="http://localhost/idevs/images/favicon.ico" />
</head>
<body>
<div id="content">
	<div id="header">
		<h1><span class="noDisplay">Idevs - That Exactly What I Do</span></h1>
	</div>
		<div id="menu">
			<ul>
			<li><a href="index.php" class="<!BlogLink!>">Blog</a></li>
			<li><a href="about.php" class="<!AboutLink!>">About</a></li>
			<li><a href="portfolio.php" class="<!PortfolioLink!>">Portfolio</a></li>
			</ul>
		</div>


	<div id="body">
		<!Content!>
	</div>
<div id="footer">
<p>All content on this site is &copy Copyright Jamie Lewis, 2006. All Right Reserved</p>
</div>
</div>
</body>
</html>
index.php
Code:
<?php
include("page.class.php");

$Title = "Blog";
$Content = "";

$Page = new IdevsPage("index.itf");
$Page->BuildReplacement("<!Title!>",$Title);
$Page->BuildReplacement("<!Content!>",$Content);
$Page->BuildReplacement("<!Style!>","blog.css");
//Links
$Page->BuildReplacement("<!BlogLink!>","Current");
$Page->BuildReplacement("<!AboutLink!>","Normal");
$Page->BuildReplacement("<!PortfolioLink!>","Normal");

$Page->PopulatePage();
$Page->LoadPage();

?>
The Module code is preetty simular except that the GetModule() function needs to return to a variable that will eventually be added to the main template by a BuildReplacement function.

If anyone needs a hand then just ask ;D

Jamie

__________________
Jamie is offline   Reply With Quote


Old 07-22-2006   #2 (permalink)
jerry
Junior Member
 
Join Date: Jul 2006
Age: 21
Posts: 14
Send a message via ICQ to jerry Send a message via AIM to jerry Send a message via MSN to jerry Send a message via Yahoo to jerry
Default Re: Code Snippet: A Simple Templating Class

im suprised no one has commented on the fact you dont use comments in your code :s and from what i can see you got better syntax then me >< mines just a big cluster **** lol
jerry is offline   Reply With Quote

Old 07-23-2006   #3 (permalink)
Jamie
Senior Member
 
Jamie's Avatar
 
Join Date: Jul 2006
Location: Swansea, Wales, UK
Age: 20
Posts: 239
Default Re: Code Snippet: A Simple Templating Class

Yeah, I normally only comment my code if I consider it a)important and b) finished. when im working with c++ i usually have comments like

Code:
//DONT edit this. It took me too long to write.
I will try and comment this at some point but most of the commands are pretty self explanitory.

Jamie
__________________
Jamie is offline   Reply With Quote

Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[PHP5] Code Snippet: Template System Suited to small scale scripts Gamelab PHP 2 10-12-2006 02:08 AM
Little Johnny in Math Class krunkfunk Jokes / Humor 0 10-03-2006 02:55 AM
[PHP5] A SQL class Seph PHP 1 07-24-2006 07:21 PM
Synchronizing file to class Tom Tom .NET 0 07-06-2006 02:06 PM
Class Room Art NuFlo Graphics & Multimedia 3 09-19-2005 12:58 AM


footer left
All times are GMT. The time now is 01:02 AM.

DISCLAIMER: preCharge Risk Management is not responsible for any opinions, advice or comments expressed on the preCharge Community Forums.
preCharge® is a registered trademark of preCharge Risk Management | chargeback protection | Merchant Account Blog

Powered by vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC6

Credit Report | Loans | Final Fantasy XI MP3 | Myspace Whore Train | Jordan Kicks

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49