6 * The Simplicity Template module provides a simple PHP based templating system.
8 * @author John Le Drew <jp@antz29.com>
9 * @copyright Copyright (c) 2009, John Le Drew
10 * @license http://www.opensource.org/licenses/mit-license.php MIT License
11 * @version 2.0.0-alpha
13 * @smp_children smp_TemplatePlugin
14 * @smp_children smp_Template
17 class smp_Template
extends smp_Pluggable
implements smp_ModuleStatic
20 private $_data = array('json_data'=>array());
21 private $_plugins = array();
28 public function __construct($path,$parent=null)
30 $this->createExtensionPoint('smp_TemplatePlugin');
31 $this->registerPluginPrefix('smp_');
33 if (!($parent instanceof smp_Template
))
35 if (!file_exists($path)) {
36 throw new Exception("{$path} does not exist. Failed to load template.");
39 if (!file_exists($path)) $path = $parent->getRoot().$path;
40 if (!file_exists($path)) {
45 $this->_template
= $path;
46 $this->_root
= ($parent instanceof smp_Template
) ?
$parent->getRoot() : dirname($this->_template
).DS
;
48 $this->_parent
= $parent;
51 private function setRoot($path)
53 $this->_root
= $path.DS
;
56 public function getRoot()
62 * Set a value to be passed to the template. This will be accessible within
63 * the template as a property ($this->name).
68 public function &set($name,$value)
70 $this->_data
[$name] = $value;
71 return $this->_data
[$name];
75 * Get a value assigned to the template.
80 public function &get($name)
82 if (($this->_parent
instanceof smp_Template
) && !$this->has($name)) {
83 return $this->_parent
->get($name);
87 if (!isset($this->_data
[$name])) return $ret;
89 return $this->_data
[$name];
93 * Add an items to the specified list, if the list does not exist a new list is created
94 * in the current template. You can also specify a key to make the list associative.
100 public function addItem($list,$value)
102 $list_ref =& $this->get($list);
105 $list_ref =& $this->set($list,array());
108 $list_ref[] = $value;
112 * Load a specific plugin and make it available to the template.
114 * You can either provide an instance or class name. By default,
115 * once loaded a plugin will be available as a method of the template
116 * by the same name ($this->MyPlugin()), alternatively you can
117 * specify an identifier as the second param.
119 * @param smp_TemplatePlugin $plugin
120 * @param string $identifier
121 * @return smp_TemplatePlugin
123 public function loadPlugin($plugin,$identifier=null)
125 if ($plugin instanceof smp_TemplatePlugin
) {
130 if (!$plugin = $this->getModuleClass($plugin)) throw new Exception("Unknown template plugin {$plugin}");
134 if (!isset($identifier)) {
135 $identifier = $this->getClassModule(get_class($p));
138 $this->_plugins
[$identifier] = $p;
140 return $this->_plugins
[$identifier];
144 * Get an instance of a loaded plugin.
146 * @param string $name
147 * @return smp_TemplatePlugin
149 public function getPlugin($name)
151 if (($this->_parent
instanceof smp_Template
) && !$this->hasPlugin($name)) {
152 return $this->_parent
->getPlugin($name);
155 if (isset($this->_plugins
[$name]))
157 return $this->_plugins
[$name];
160 return $this->loadPlugin($name);
163 public function hasPlugin($name)
165 return isset($this->_plugins
[$name]);
169 * Allows access to plugins via a overloaded method.
173 public function __call($name,$args)
175 return $this->getPlugin($name);
179 * Allows access to template data via a overloaded property.
183 public function __get($name)
185 return $this->get($name);
189 * Returns boolean true / false if the specified key is available..
191 * @param string $name
194 public function has($name)
196 return isset($this->_data
[$name]);
200 * __isset overloaded function.
205 public function __isset($name)
207 return $this->has($name);
211 * Render the template at the given path and return a string.
213 * @param string $path
216 public function render($path,$args=array())
218 if (substr($path,0,2) == './') {
219 $path = dirname($this->_template
).DS
.substr($path,2);
221 $tpl = new smp_Template($path,$this);
222 foreach ($args as $k => $v)
226 return $tpl->renderTemplate();
230 * Render the root template and return the resulting string.
234 public function renderTemplate()
236 if (!($this->_parent
instanceof smp_Template
))
238 if (ob_get_length()) ob_end_clean();
242 if (file_exists($this->_template
))
244 if (substr($this->_template
,0,7) == 'smpa://')
246 include smp_Archive
::loadFileFromArchive($this->_template
,true);
250 include $this->_template
;
254 return ob_get_clean();