-
Notifications
You must be signed in to change notification settings - Fork 70
/
Template.php
127 lines (115 loc) · 3.59 KB
/
Template.php
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* This file is part of the php-apidoc package.
*/
namespace Crada\Apidoc;
/**
* Class imported from https://github.com/isRuslan/php-template
* @author Ruslan Ismagilov <[email protected]>
*
* @license http://opensource.org/licenses/bsd-license.php The BSD License
* @author Calin Rada <[email protected]>
*/
class Template {
/**
* Content variables
* @access private
* @var array
*/
private $vars = [];
/**
* Content delimiters
* @access private
* @var string
*/
private $l_delim = '{{',
$r_delim = '}}';
/**
* Set template property in template file
* @access public
* @param string $key property name
* @param string $value property value
*/
public function assign( $key, $value )
{
$this->vars[$key] = $value;
}
/**
* Parse template file
* @access public
* @param string $template_file
*/
public function parse( $template_file )
{
if ( file_exists( $template_file ) ) {
$content = file_get_contents($template_file);
foreach ( $this->vars as $key => $value ) {
if ( is_array( $value ) ) {
$content = $this->parsePair($key, $value, $content);
} else {
$content = $this->parseSingle($key, (string) $value, $content);
}
}
return $content;
} else {
exit( '<h1>Template error</h1>' );
}
}
/**
* Parsing content for single varliable
* @access private
* @param string $key property name
* @param string $value property value
* @param string $string content to replace
* @param integer $index index of loop item
* @return string replaced content
*/
private function parseSingle( $key, $value, $string, $index = null )
{
if ( isset( $index ) ) {
$string = str_replace( $this->l_delim . '%index%' . $this->r_delim, $index, $string );
}
return str_replace( $this->l_delim . $key . $this->r_delim, $value, $string );
}
/**
* Parsing content for loop varliable
* @access private
* @param string $variable loop name
* @param string $value loop data
* @param string $string content to replace
* @return string replaced content
*/
private function parsePair( $variable, $data, $string )
{
$match = $this->matchPair($string, $variable);
if( $match == false ) return $string;
$str = '';
foreach ( $data as $k_row => $row ) {
$temp = $match['1'];
foreach( $row as $key => $val ) {
if( !is_array( $val ) ) {
$index = array_search( $k_row, array_keys( $data ) );
$temp = $this->parseSingle( $key, $val, $temp, $index );
} else {
$temp = $this->parsePair( $key, $val, $temp );
}
}
$str .= $temp;
}
return str_replace( $match['0'], $str, $string );
}
/**
* Match loop pair
* @access private
* @param string $string content with loop
* @param string $variable loop name
* @return string matched content
*/
private function matchPair( $string, $variable )
{
if ( !preg_match("|" . preg_quote($this->l_delim) . 'loop ' . $variable . preg_quote($this->r_delim) . "(.+?)". preg_quote($this->l_delim) . 'end loop' . preg_quote($this->r_delim) . "|s", $string, $match ) ) {
return false;
}
return $match;
}
}