Skip to content
Permalink
Browse files

Add push/pop onto StringTemplate

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 e85e241278444c43f5870daf2ee1b87487dfd578
Showing with 52 additions and 2 deletions.
  1. +33 −2 src/View/StringTemplate.php
  2. +19 −0 tests/TestCase/View/StringTemplateTest.php
@@ -51,6 +51,13 @@ class StringTemplate {
'compactAttribute' => '{{name}}="{{value}}"',
];
/**
* A stack of template sets that have been stashed temporarily.
*
* @var
*/
protected $_configStack = [];
/**
* Contains the list of compiled templates
*
@@ -67,15 +74,39 @@ public function __construct(array $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
*
* ### Example:
*
* {{{
* $templater->add([
* 'link' => '<a href="{{url}}">{{title}}</a>'
* 'button' => '<button>{{text}}</button>'
* 'link' => '<a href="{{url}}">{{title}}</a>'
* 'button' => '<button>{{text}}</button>'
* ]);
* }}}
*
@@ -209,4 +209,23 @@ public function testCopiledInfoRefresh() {
$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.
You can’t perform that action at this time.