Creating a template system
Moderator: General Moderators
Creating a template system
I'm trying to create a template system but haven't got a good idea. What I though was I can open the template file and put it into an array and use ereg_replace or something to replace the text. Do you think there is (or are) better way of doing it?
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
I've created it but..
Now I've made the following code for the template thing. Do you think this would work...?
Thanks.
Code: Select all
<?php
/*
Template Function Library
*/
class class_template {
var $root_folder; //Stores root folder of template files
var $file_content; //Stores the content of tempalte files
var $filenames; //Stores the names of template files
var $variables; //Stores the variable name for the template
var $num;
function define($folder,$template_variables) {
if(is_dir($folder)) {
$this->root_folder = $folder;
} else {
$this->error_report("The root directory specified is not a directory!");
exit();
}
$this->num = 0;
while(list($key,$value) = each($template_variables)) {
if(!file_exists($this->root_folder.$value)) {
$this->error_report("$value does not exists!");
exit();
}
$this->filenamesї$i] = $value;
$this->variablesї$i] = fopen($value,"r");
$keyї$i] = $key;
$this->num++;
}
$this->num = $this->num - 1;
}
function assign($template_vairable,$value) {
for($i = 0; $i == $this->num; $i++) {
$this->file_contentї$i] = fread($this->vairablesї$i],filesize($this->filenamesї$i]));
if(strstr($this->variablesї$i])) {
$this->file_contentї$i] = str_replace("{$template_vairable}",$value,$this->file_content);
}
}
unset($i);
}
function print_print($order) {
$order = explode(",",$order);
$order_num = count($order);
$i = 0; $n = 0;
while($i != $this->num) {
while($n != $this->num) {
if($orderї$i] == $file_contentї$n]) {
echo $file_contentї$n];
$n = 0;
return false;
}
}
}
}
function error_report($error_msg) {
echo "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Unexpected error has occured</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div align="center"> <span style="font-family: Verdana, Arial, Helvetica, sans-serif; color: #FF0000; font-size: 12px; font-weight: bold;">".$error_msg."</span>
</div>
</body>
</html>";
}
}
?>- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
After trying
Here's what I got till but I have a problem...
in the function "assign" it tries to access $this->filename and $this->variable but it seems that it's empty. I tried accessing it on function "define" and it holds a value. This is causing the following error when I execute with the following code. The code above is called template.php and under a folder called "library"
When I execute this I get this error:-

Code: Select all
<?php
/*
Template Function Library
*/
class class_template {
var $root_folder; //Stores root folder of template files
var $file_content; //Stores the content of tempalte files
var $filenames; //Stores the names of template files
var $variables; //File Handle for the template file
var $key; //Stores the variable for tempalte
var $num;
function define($folder,$template_variables) {
if(is_dir($folder)) {
$this->root_folder = $folder;
} else {
$this->error_report("The root directory specified is not a directory!");
exit();
}
$this->num = 0;
while(list($key,$value) = each($template_variables)) {
if(!file_exists($this->root_folder."/".$value)) {
$this->error_report("$value does not exists!");
exit();
}
$this->filenamesї$i] = $value;
$this->variablesї$i] = fopen($this->root_folder."/".$value,"r");
$this->keyї$i] = $key;
$this->num++;
}
}
function assign($template_vairable,$value) {
for($i = 0; $i != $this->num; $i++) {echo $this->filenamesї$i];;
$this->file_contentї$i] = fread($this->vairablesї$i],filesize($this->filenamesї$i]));
$this->file_contentї$i] = str_replace("{$template_vairable}",$value,$this->file_content);
}
unset($i);
}
function print_print($order) {
$order = explode(",",$order);
$order_num = count($order);
$i = 0; $n = 0;
while($i != $this->num) {
while($n != $this->num) {
if($orderї$i] == $file_contentї$n]) {
echo $file_contentї$n];
$n = 0;
return false;
}
}
}
}
function error_report($error_msg) {
echo "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Unexpected error has occured</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div align="center"> <span style="font-family: Verdana, Arial, Helvetica, sans-serif; color: #FF0000; font-size: 12px; font-weight: bold;">".$error_msg."</span>
</div>
</body>
</html>";
}
}
?>Code: Select all
<?php
include("library/template.php");
$tpl = new class_template();
$tpl->define("library",array("basic" => "tpl.tpl"));
$tpl->assign("CONTENT","hey");
?>Now I have not got any idea why $this->filenames and $this->variables becomes empty... Please helpPHP Parser wrote:Warning: stat failed for (errno=2 - No such file or directory) in /usr/home/ticktaku/public_html/library/template.php on line 43
Warning: fread(): supplied argument is not a valid File-Handle resource in /usr/home/ticktaku/public_html/library/template.php on line 43
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
Code: Select all
$this->filenamesї$i] = $value;
$this->variablesї$i] = fopen($this->root_folder."/".$value,"r");
$this->keyї$i] = $key;there is no $i variable
Few one final step
After thinking for a while I've come up with this code (hot_goblin thanks alot
)
This does everything fine except that when replacing {$template_variable} to $value the { and } still remains, why is that?
Code: Select all
<?php
/*
Template Function Library
*/
class class_template {
var $root_folder; //Stores root folder of template files
var $file_content; //Stores the content of tempalte files
var $filenames; //Stores the names of template files
var $variables; //File Handle for the template file
var $key; //Stores the variable for tempalte
var $num;
function define($folder,$template_variables) {
if(is_dir($folder)) {
$this->root_folder = $folder;
} else {
$this->error_report("The root directory specified is not a directory!");
exit();
}
$this->num = 0;
while(list($key,$value) = each($template_variables)) {
if(!file_exists($this->root_folder."/".$value)) {
$this->error_report("$value does not exists!");
exit();
}
$this->filenamesї$this->num] = $value;
$this->variablesї$this->num] = fopen($this->root_folder."/".$value,"r");
$this->keyї$this->num] = $key;
$this->num++;
}
}
function assign($template_vairable,$value) {
for($i = 0; $i != $this->num; $i++) {
$this->file_contentї$i] = fread($this->variablesї$i],filesize($this->root_folder."/".$this->filenamesї$i]));
$this->file_contentї$i] = str_replace("{$template_vairable}",$value,$this->file_content);
}
unset($i);
}
function print_template($order) {
$order = explode(",",$order);
$order_num = count($order);
$order_num = $order_num - 1;
for($i = 0; $i <= $order_num; $i++) {
for($n = 0; $n <= $i; $n++) {
if($this->keyї$n] == $orderї$i]) {
list($content) = $this->file_contentї$i];
echo $content;
}
}
}
}
function error_report($error_msg) {
echo "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Unexpected error has occured</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div align="center"> <span style="font-family: Verdana, Arial, Helvetica, sans-serif; color: #FF0000; font-size: 12px; font-weight: bold;">".$error_msg."</span>
</div>
</body>
</html>";
}
}
?>- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact: