forked from levhita/ThaFrame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplatePattern.php
137 lines (121 loc) · 3.59 KB
/
TemplatePattern.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
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Holds {@link TemplatePattern} class
* @package ThaFrame
* @author Argel Arias <[email protected]>
* @copyright Copyright (c) 2007, Argel Arias <[email protected]>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
/**
* Provide basic template system
* @package ThaFrame
*/
class TemplatePattern
{
/**
* Holds the variables to be passed to the template as $Data object
* @var array
*/
protected $_variables = array();
/**
* Holds the javascripts that should be added at the head section
* @var array
*/
protected $_javascripts = array();
/**
* Holds the relative path to the template
* @var string
*/
protected $_template = '';
/**
* Variables that belongs only to this pattern, used to customize the text and
* appareance of the page
* @var array
*/
protected $_pattern_variables = array();
public function __construct($template='')
{
if ( empty($template) ) {
$template = $this->getScriptName();
}
$this->setTemplate($template);
}
/**
* Adds a javascript that will be added in the head section
* @param string $javascript the javascript code
* @return void
*/
public function addJavascript($javascript)
{
$this->_javascripts[] = $javascript;
}
/**
* Sets the template file to be used.
*
* Take for granted that the file is under the relative path "templates/" and
* has a "tpl.php" extension, unless you set $fullpath to true
* @param string $template The name of the template to be used
* @param bool $fullpath overrides the naming convention and allows you to set any file
* @return void
*/
public function setTemplate($template, $fullpath = false)
{
if ( !$fullpath) {
$this->_template = "templates/$template.tpl.php";
} else {
$this->_template = $template;
}
}
public function assign($variable, $value)
{
$this->_variables[$variable] = $value;
}
public function getScriptName(){
return basename($_SERVER['SCRIPT_FILENAME'], '.php');
}
/**
* Sets a pattern specific variable, variables set by this function aren't
* mandatory, and are only to provide customization to the default template
*
* @param string $variable the variable to be set
* @param string $value the content that will override the default value
* @return void
*/
public function setPatternVariable($variable, $value) {
$this->_pattern_variables[$variable] = $value;
}
/**
* Shows the given template
*
* Converts the $variables array into $Data object and sets any message that may
* be in the $_SESSION and finally calls the given template
* @return void
*/
public function getAsString()
{
if ( !file_exists($this->_template) ) {
throw new InvalidArgumentException("template '$this->_template' doesn't exists");
}
$this->assign('__PatternVariables', (object)$this->_pattern_variables);
$this->assign('__javascripts', $this->_javascripts);
return self::runTemplate($this->_template, $this->_variables);
}
/**
* Run the Template in the cleanest enviroment posible
* @param string $template
* @param array $data
* @return string
*/
protected static function runTemplate($_template_, $_data_) {
$Helper = new HelperPattern((object)$_data_);
extract($_data_);
if ( !file_exists($_template_) ) {
throw new InvalidArgumentException("template '$_template_' doesn't exists");
}
ob_start();
include $_template_;
$_content_ = ob_get_contents();
ob_end_clean();
return $_content_;
}
}