pre move to sub-modules, probably broken
[simplicity.git] / source / lib / simplicity / modules / template / template.php
blob05dcddaff838e9aaf8743b2dbdf8f3a19087705e
1 <?php
3 /**
4 * Template Module
5 *
6 * The Simplicity Template module provides a simple PHP based templating system.
7 *
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
15 * @smp_core
17 class smp_Template extends smp_Pluggable implements smp_ModuleStatic
19 private $_path;
20 private $_data = array('json_data'=>array());
21 private $_plugins = array();
23 private $_parent;
25 private $_template;
26 private $_root;
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.");
38 } else {
39 if (!file_exists($path)) $path = $parent->getRoot().$path;
40 if (!file_exists($path)) {
41 return;
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()
58 return $this->_root;
61 /**
62 * Set a value to be passed to the template. This will be accessible within
63 * the template as a property ($this->name).
65 * @param string $name
66 * @param mixed $value
68 public function &set($name,$value)
70 $this->_data[$name] = $value;
71 return $this->_data[$name];
74 /**
75 * Get a value assigned to the template.
77 * @param string $name
78 * @return mixed
80 public function &get($name)
82 if (($this->_parent instanceof smp_Template) && !$this->has($name)) {
83 return $this->_parent->get($name);
85 $ret = null;
87 if (!isset($this->_data[$name])) return $ret;
89 return $this->_data[$name];
92 /**
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.
96 * @param $list string
97 * @param $value mixed
98 * @return void
100 public function addItem($list,$value)
102 $list_ref =& $this->get($list);
104 if (!$list_ref) {
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) {
126 $p = $plugin;
128 else
130 if (!$plugin = $this->getModuleClass($plugin)) throw new Exception("Unknown template plugin {$plugin}");
131 $p = new $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.
171 * @see getPlugin
173 public function __call($name,$args)
175 return $this->getPlugin($name);
179 * Allows access to template data via a overloaded property.
181 * @see get
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
192 * @return boolean
194 public function has($name)
196 return isset($this->_data[$name]);
200 * __isset overloaded function.
202 * @see has
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
214 * @return string
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)
224 $tpl->set($k,$v);
226 return $tpl->renderTemplate();
230 * Render the root template and return the resulting string.
232 * @return string
234 public function renderTemplate()
236 if (!($this->_parent instanceof smp_Template))
238 if (ob_get_length()) ob_end_clean();
241 ob_start();
242 if (file_exists($this->_template))
244 if (substr($this->_template,0,7) == 'smpa://')
246 include smp_Archive::loadFileFromArchive($this->_template,true);
248 else
250 include $this->_template;
254 return ob_get_clean();