Permalink
Browse files

updating the Template class to handle things View used to do

  • Loading branch information...
1 parent 89b9611 commit d52033f709befb16190ff2e324004af2aed1e5b4 @enygma committed Aug 10, 2012
Showing with 122 additions and 3 deletions.
  1. +122 −3 Shield/Template.php
View
125 Shield/Template.php
@@ -6,10 +6,15 @@ class Template extends Base
{
private $config = null;
private $templateDir = null;
+ private $filter = null;
+ private $charset = 'UTF-8';
+ private $contentType = 'text/html';
- public function __construct(\Shield\Config $config)
+ public function __construct(\Shield\Config $config, \Shield\Filter $filter)
{
$this->config = $config;
+ $this->filter = $filter;
+
$this->setTemplateDir();
}
@@ -42,7 +47,64 @@ public function __get($name)
*/
public function __set($name,$value)
{
- $this->_properties[$name] = $value;
+ $this->set($name, $value);
+ }
+
+ /**
+ * Set the value(s) to the template
+ *
+ * @param string $index Property name
+ * @param mixed $value Property value
+ *
+ * @return object $this Current Template instance
+ */
+ public function set($index, $value=null)
+ {
+ if (!is_array($index)) {
+ $index = array($index => $value);
+ }
+ foreach ($index as $i => $value) {
+ $this->_properties[$i] = $value;
+ }
+
+ return $this;
+ }
+
+ /**
+ * Add a filter to a property
+ *
+ * @param string $index Property name
+ * @param mixed $filter Either a filter type or a closure
+ *
+ * @return object $this Template instance
+ */
+ public function filter($index, $filter=null)
+ {
+ if (!is_array($index)) {
+ $index = array($index => $filter);
+ }
+ foreach ($index as $i => $value) {
+ if (is_array($value)) {
+ foreach ($value as $f) {
+ $this->filter->add($i, $f);
+ }
+ } else {
+ $this->filter->add($i, $filter);
+ }
+ }
+ return $this;
+ }
+
+ /**
+ * Get a value from our properties
+ *
+ * @param string $index Index name of property
+ *
+ * @return mixed Either NULL or the property value, if found
+ */
+ public function get($index)
+ {
+ return (isset($this->_properties[$index])) ? $this->_properties[$index] : null;
}
/**
@@ -74,6 +136,50 @@ public function setTemplateDir($dir=null)
}
/**
+ * Get the current content type value
+ *
+ * @return string Content type
+ */
+ public function getContentType()
+ {
+ return $this->contentType;
+ }
+
+ /**
+ * Set the content type (ex. "text/html")
+ *
+ * @param string $contentType Content type string
+ *
+ * @return object $this Template
+ */
+ public function setContentType($contentType)
+ {
+ $this->contentType = $contentType;
+ return $this;
+ }
+
+ /**
+ * Get the current character set
+ *
+ * @return string Character set
+ */
+ public function getCharset()
+ {
+ return $this->charset;
+ }
+
+ /**
+ * Set the character set
+ *
+ * @param string $charset Character set (ex. "UTF-8")
+ */
+ public function setCharset($charset)
+ {
+ $this->charset = $charset;
+ return $this;
+ }
+
+ /**
* Render the template - either using a file (views/) or as a string
* Checks to see if the $template references a file first
*
@@ -83,9 +189,19 @@ public function setTemplateDir($dir=null)
*/
public function render($template)
{
+ $charset = $this->getCharset();
+ $contentType = $this->getContentType();
+
+ header('Content-Type: '.$contentType.'; charset='.$charset);
+
// first see if what we've been given is a file
$templateFile = $this->getTemplateDir().'/'.$template.'.php';
+ // run through our properties and filter
+ foreach ($this->_properties as $index => $value) {
+ $this->_properties[$index] = $this->filter->filter($index,$value);
+ }
+
if (is_file($templateFile)) {
extract($this->_properties);
ob_start();
@@ -95,8 +211,11 @@ public function render($template)
} else {
// it's just a string! fall back on str_replace
foreach ($this->_properties as $name => $value) {
- $template = str_replace('['.$name.']',$value,$template);
+ $template = str_replace('['.$name.']', $value, $template);
}
+
+ // replace any leftovers
+ $template = preg_replace('#\[.*?\]#', '', $template);
return $template;
}
}

0 comments on commit d52033f

Please sign in to comment.