Skip to content
Browse files

updating Template and tests for new injection method

  • Loading branch information...
1 parent b7026ef commit 9a5288ba4b8241edda394bf61962d0ca7d5faf08 Chris Cornutt committed
Showing with 42 additions and 6 deletions.
  1. +31 −3 Shield/Template.php
  2. +5 −2 Shield/tests/Shield/TemplateTest.php
  3. +6 −1 Shield/tests/Shield/ViewTest.php
View
34 Shield/Template.php
@@ -4,11 +4,13 @@
class Template extends Base
{
- private $config = null;
+ private $config = null;
+ private $templateDir = null;
public function __construct(\Shield\Config $config)
{
$this->config = $config;
+ $this->setTemplateDir();
}
/**
@@ -44,6 +46,33 @@ public function __set($name,$value)
}
/**
+ * Get the current templates directory
+ *
+ * @return string Full path to templates directory
+ */
+ public function getTemplateDir()
+ {
+ return $this->templateDir;
+ }
+
+ /**
+ * Set the directory where the templates live
+ *
+ * @param string $dir Directory path
+ *
+ * @return null
+ */
+ public function setTemplateDir($dir=null)
+ {
+ // see if the path is valid
+ $templatePath = ($dir !== null) ? $dir : __DIR__.'/../app/views';
+
+ if (realpath($templatePath) !== false) {
+ $this->templateDir = realpath($templatePath);
+ }
+ }
+
+ /**
* Render the template - either using a file (views/) or as a string
* Checks to see if the $template references a file first
*
@@ -54,8 +83,7 @@ public function __set($name,$value)
public function render($template)
{
// first see if what we've been given is a file
- $templateFile = $this->di->get('View')->getViewDir().
- '/'.$template.'.php';
+ $templateFile = $this->getTemplateDir().'/'.$template.'.php';
if (is_file($templateFile)) {
extract($this->_properties);
View
7 Shield/tests/Shield/TemplateTest.php
@@ -6,18 +6,21 @@ class TemplateTest extends \PHPUnit_Framework_TestCase
{
private $_template = null;
private $_di = null;
+ private $_config = null;
public function setUp()
{
$this->_di = new Di();
- $this->_di->register(new View($this->_di));
+ $this->_config = new Config($this->_di);
+ $this->_template = new Template($this->_config);
- $this->_template = new Template($this->_di);
+ $this->_di->register(new View($this->_config, $this->_template));
}
public function tearDown()
{
$this->_di = null;
$this->_template = null;
+ $this->_config = null;
}
/**
View
7 Shield/tests/Shield/ViewTest.php
@@ -6,16 +6,21 @@ class ViewTest extends \PHPUnit_Framework_TestCase
{
private $_view = null;
private $_di = null;
+ private $_config = null;
public function setUp()
{
$this->_di = new Di();
- $this->_view = new View($this->_di);
+ $this->_config = new Config($this->_di);
+ $template = new Template($this->_config);
+
+ $this->_view = new View($this->_config, $template);
}
public function tearDown()
{
$this->_di = null;
$this->_view = null;
+ $this->_config = null;
}
/**

0 comments on commit 9a5288b

Please sign in to comment.
Something went wrong with that request. Please try again.