Skip to content

Commit

Permalink
updating Template and tests for new injection method
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Cornutt committed Aug 8, 2012
1 parent b7026ef commit 9a5288b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
34 changes: 31 additions & 3 deletions Shield/Template.php
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -43,6 +45,33 @@ public function __set($name,$value)
$this->_properties[$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
Expand All @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions Shield/tests/Shield/TemplateTest.php
Expand Up @@ -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;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion Shield/tests/Shield/ViewTest.php
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 9a5288b

Please sign in to comment.