Permalink
Browse files

Merge branch 'master' of github.com:sparkfun/SparkLib into endicia_7

  • Loading branch information...
2 parents 40eab01 + 07968e8 commit 6dcd9b8af01ae52b1b5513f761c943cff23defb1 @toddtreece toddtreece committed Apr 15, 2013
Showing with 15 additions and 3 deletions.
  1. +15 −3 Template.php
View
@@ -53,7 +53,7 @@ class Template extends HTML implements Renderable {
/**
* Array of properties/variables available in template.
*/
- protected $_context;
+ protected $_context = array();
/**
* Create a new template.
@@ -65,12 +65,13 @@ class Template extends HTML implements Renderable {
* @param array optional array of variables to extract() before template is require'd
* @param boolean cache template contents?
*/
- public function __construct ($template = null, $context = array())
+ public function __construct ($template = null, $context = null)
{
$this->_templateDir = \LIBDIR . 'templates';
if ($template)
$this->setTemplate($template);
- $this->setContext($context);
+ if (is_array($context))
+ $this->setContext($context);
}
/**
@@ -126,21 +127,32 @@ public function setContext ($context = array())
$this->_context = $context;
elseif ($context instanceof Template)
$this->_context = $context->getContext();
+ else
+ throw new \UnexpectedValueException('context must be array or Template instance');
return $this;
}
/**
* Add a set of values to existing context. Overwrites existing values
* if the given ones happen to overlap, leaves existing ones in place.
+ *
+ * If given an instance of Template, will copy that template's entire
+ * context.
+ *
+ * @return $this
*/
public function addContext ($context = array())
{
if ($context instanceof Template)
$context = $context->getContext();
+ if (! is_array($context))
+ throw new \UnexpectedValueException('context must be an array or Template instance');
+
foreach ($context as $key => $val) {
$this->__set($key, $val);
}
+
return $this;
}

0 comments on commit 6dcd9b8

Please sign in to comment.