-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoSimpleTemplate.php
More file actions
80 lines (77 loc) · 1.5 KB
/
coSimpleTemplate.php
File metadata and controls
80 lines (77 loc) · 1.5 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/* example usage
some.php>>>
<?php
$template = new coSimpleTemplate("rute/to/archive.tpl");
$template->set("foo", "bar");
$template->set("foo2", 1);
print $template->output();
?>
rute/to/archive.tpl>>>
<html><head>
<title>[@foo]</title>
</head>
<body>
Human quanty: [@foo2]
</body>
</html>
Result>>
<html><head>
<title>bar</title>
</head>
<body>
Human quanty: 1
</body>
</html>
*/
class coSimpleTemplate {
/**
* a Simple PHP Template Motor
* Perfect for "embended code"
* remplase the tag whith form [@tag] by value
* @package coSimpleTemplate
*/
/**
* the absolute name of the template
* @access private
* @var string
*/
protected $file;
/**
* save the relations of "tags/values"
* @access private
* @var array
*/
protected $values = array();
/**
* Constructor sets up {@link $file}
*/
public function __construct($file) {
$this->file = $file;
}
/**
* set in {@link $values} the relations "keys/values"
* @param string|integer $key the key to declare (remplaced in template a [@tag]
* @param string|integer $value the value to set
*/
public function set($key, $value) {
$this->values[$key] = $value;
}
/**
* Take {@link $values} data
* and change the tags in {@link $file}
* and return the result
* @return string
*/
public function output() {
if (!file_exists($this->file)) {
return 'Error loading template file (' .$this->file. ').' . "\r\n";
}
$output = file_get_contents($this->file);
foreach ($this->values as $key => $value) {
$tag = '[@' . $key . ']';
$output = str_replace($tag, $value, $output);
}
return $output;
}
}