MY PHP TEMPLATE ENGIN

Links for php scripts

Moderators: macek, egami, gesf

aatifbangash
New php-forum User
New php-forum User
Posts: 6
Joined: Mon Jul 25, 2011 5:33 am

MY PHP TEMPLATE ENGIN

Postby aatifbangash » Sat Oct 29, 2011 6:53 am

TemplateClass.php file...............

<?php
class temp{
private $css;
private $js;
private $title;
private $heading;
private $body;
public function __construct($css="",$js=""){
if(!empty($css) && !empty($js)){
if(!file_exists($css) || !file_exists($js)){
echo "<p>css or js file not found</p>";
exit;
}
}
$this->css=$css;
$this->js=$js;
return true;
}
private function set($title,$heading){
$this->title=$title;
$this->heading=$heading;
return true;
}
private function startBody(){
ob_start();
}
private function endBody(){
$this->body=ob_get_clean();
return true;
}
private function render($path){
if(file_exists($path)){
include("$path");
return true;
}else{
echo "template file not found<br />";
exit;
return false;
}
}
public function start($title,$heading){
$this->set($title,$heading);
$this->startBody();
}
public function finish($temp_path){
$this->endBody();
$this->render($temp_path);
}
public function addvar($var,$val){
$allvars=get_object_vars($this);
if(array_key_exists($var,$allvars)){ echo "var \${$var} already declared<br />"; exit;}
$this->$var=$val;
return $this->$var;
}
}
$page=new temp("style.css","js.js");
$date=date("D M Y",time());
$page->addvar("now",$date);
?>
---------------------------------------------------------------------------------------

template.php file..............

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $this->title; ?></title>
<script type="text/javascript" src="<?php if(isset($this->js)){ echo $this->js;} ?>">
</script>
<link rel="stylesheet" type="text/javascript" href="<?php if(isset($this->css)){ echo $this->css;} ?>" />
</head>

<body>
<p align="right"><?php echo "TODAY DATE IS: ". $this->now; ?></p>
<div id="heading">
<?php echo strtoupper($this->heading); ?>
</div>
<?php echo $this->body; ?>
</body>
</html>
---------------------------------------------------------------------

index.php file.............

<?php
include("temp.php");
$page->start("temp","temp");

?>
<div>
<form>
<input type="text" ><br />
<input type="submit" value="send" />
</form>
</div>
<?php echo $page->now; ?>
<?php
$page->finish("template.php");

?>


be happy :D

andor1995
New php-forum User
New php-forum User
Posts: 15
Joined: Mon Jan 09, 2012 11:39 pm

Re: MY PHP TEMPLATE ENGIN

Postby andor1995 » Thu Jan 26, 2012 9:03 am

Does this allowes us to change the websites theme?


Return to “PHP Scripts”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest