-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.php
More file actions
59 lines (49 loc) · 1.58 KB
/
template.php
File metadata and controls
59 lines (49 loc) · 1.58 KB
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
50
51
52
53
54
55
56
57
58
59
<?php
/********************************************
Document : Template Class
Created on : February 23, 2013, 9:18 AM
Author : Tauseef Jamadar
Description:
Template Class to manage and render any template
*********************************************/
/**
* Template Class to manage any template
* Note: templates must be in html format and variables must be present within []
*/
class template {
/*
* private collection of variables not to be expose
*/
private $variableCollection = array();
/**
* Assign values to the template variables
* @param type $key key name for the template variable
* @param type $value value for the key template variable
*/
public function assign($key, $value)
{
$this->variableCollection[$key] = $value;
}
/**
* Renders the specified template by replacing the variables with their corresponding values
* @param type $templateName Template to be rendered
*/
public function renderTemplate($templateName)
{
$path = $templateName.'.html';
if(file_exists($path))
{
$contents = file_get_contents($path);
//Replace the variables in template with their values
foreach ($this->variableCollection as $key => $value)
{
$contents = preg_replace('/\['.$key.'\]/', $value, $contents);
}
eval ('?>'.$contents.'<?php ');
}
else {
exit('<h1>Template Error !</h1>');
}
}
}
?>