Skip to content

Commit

Permalink
Add push/pop onto StringTemplate
Browse files Browse the repository at this point in the history
These methods will make managing templates simpler in FormHelper which
needs to manage templates temporarily.
  • Loading branch information
markstory committed Jul 23, 2014
1 parent 63215b0 commit e85e241
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/View/StringTemplate.php
Expand Up @@ -51,6 +51,13 @@ class StringTemplate {
'compactAttribute' => '{{name}}="{{value}}"', 'compactAttribute' => '{{name}}="{{value}}"',
]; ];


/**
* A stack of template sets that have been stashed temporarily.
*
* @var
*/
protected $_configStack = [];

/** /**
* Contains the list of compiled templates * Contains the list of compiled templates
* *
Expand All @@ -67,15 +74,39 @@ public function __construct(array $config = []) {
$this->config($config); $this->config($config);
} }


/**
* Push the current templates onto the template stack.
*
* @return void
*/
public function push() {
$this->_configStack[] = $this->_config;
}

/**
* Restore the most recently pushed set of templates.
*
* Restoring templates will invalidate all compiled templates.
*
* @return void
*/
public function pop() {
if (empty($this->_configStack)) {
return;
}
$this->_config = array_pop($this->_configStack);
$this->_compiled = [];
}

/** /**
* Registers a list of templates by name * Registers a list of templates by name
* *
* ### Example: * ### Example:
* *
* {{{ * {{{
* $templater->add([ * $templater->add([
* 'link' => '<a href="{{url}}">{{title}}</a>' * 'link' => '<a href="{{url}}">{{title}}</a>'
* 'button' => '<button>{{text}}</button>' * 'button' => '<button>{{text}}</button>'
* ]); * ]);
* }}} * }}}
* *
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/View/StringTemplateTest.php
Expand Up @@ -209,4 +209,23 @@ public function testCopiledInfoRefresh() {
$this->assertEquals([null, null], $this->template->compile('link')); $this->assertEquals([null, null], $this->template->compile('link'));
} }


/**
* test push/pop templates.
*
* @return void
*/
public function testPushPopTemplates() {
$this->template->add(['name' => '{{name}} is my name']);
$this->assertNull($this->template->push());

$this->template->add(['name' => 'my name']);
$this->assertEquals('my name', $this->template->get('name'));

$this->assertNull($this->template->pop());
$this->assertEquals('{{name}} is my name', $this->template->get('name'));

$this->assertNull($this->template->pop());
$this->assertNull($this->template->pop());
}

} }

0 comments on commit e85e241

Please sign in to comment.