2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * hold PMA\libraries\Template class
6 * @package PMA\libraries
8 namespace PMA\libraries
;
13 * Handle front end templating
15 * @package PMA\libraries
20 * Name of the template
22 protected $name = null;
25 * Data associated with the template
30 * Helper functions for the template
32 protected $helperFunctions;
34 const BASE_PATH
= 'templates/';
37 * Template constructor
39 * @param string $name Template name
40 * @param array $data Variables to be provided to the template
41 * @param array $helperFunctions Helper functions to be used by template
43 protected function __construct($name, $data = array(), $helperFunctions = array())
47 $this->helperFunctions
= $helperFunctions;
53 * @param string $name Template name
54 * @param array $data Variables to be provided to the template
55 * @param array $helperFunctions Helper functions to be used by template
59 public static function get($name, $data = array(), $helperFunctions = array())
61 return new Template($name, $data, $helperFunctions);
65 * Adds more entries to the data for this template
67 * @param array|string $data containing data array or data key
68 * @param string $value containing data value
70 public function set($data, $value = null)
72 if(is_array($data) && ! $value) {
73 $this->data
= array_merge(
77 } else if (is_string($data)) {
78 $this->data
[$data] = $value;
83 * Adds a function for use by the template
85 * @param string $funcName function name
86 * @param callable $funcDef function definition
88 public function setHelper($funcName, $funcDef)
90 if (! isset($this->helperFunctions
[$funcName])) {
91 $this->helperFunctions
[$funcName] = $funcDef;
93 throw new \
LogicException(
94 'The function "' . $funcName . '" is already associated with the template.'
102 * @param string $funcName function name
104 public function removeHelper($funcName)
106 if (isset($this->helperFunctions
[$funcName])) {
107 unset($this->helperFunctions
[$funcName]);
109 throw new \
LogicException(
110 'The function "' . $funcName . '" is not associated with the template.'
116 * Magic call to locally inaccessible but associated helper functions
118 * @param string $funcName function name
119 * @param array $arguments function arguments
121 public function __call($funcName, $arguments)
123 if (isset($this->helperFunctions
[$funcName])) {
124 return call_user_func_array($this->helperFunctions
[$funcName], $arguments);
126 throw new \
LogicException(
127 'The function "' . $funcName . '" is not associated with the template.'
135 * @param array $data Variables to be provided to the template
136 * @param array $helperFunctions Helper functions to be used by template
140 public function render($data = array(), $helperFunctions = array())
142 $template = static::BASE_PATH
. $this->name
. '.phtml';
145 $this->helperFunctions
= array_merge(
146 $this->helperFunctions
,
149 extract($this->data
);
151 if (@file_exists
($template)) {
154 throw new \
LogicException(
155 'The template "' . $template . '" not found.'
158 $content = ob_get_clean();
161 } catch (\LogicException
$e) {
163 throw new \
LogicException($e->getMessage());