diff --git a/Cake/Test/TestCase/View/Helper/StringTemplateTraitTest.php b/Cake/Test/TestCase/View/Helper/StringTemplateTraitTest.php index 08c4ea7e6dd..3ed4728a31d 100644 --- a/Cake/Test/TestCase/View/Helper/StringTemplateTraitTest.php +++ b/Cake/Test/TestCase/View/Helper/StringTemplateTraitTest.php @@ -1,7 +1,5 @@ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * @@ -21,7 +19,7 @@ use Cake\View\Helper\StringTemplate; /** - * PaginatorHelperTest class + * StringTemplateTraitTest class * */ class StringTemplateTraitTest extends TestCase { @@ -29,19 +27,40 @@ class StringTemplateTraitTest extends TestCase { use StringTemplateTrait; /** - * testInitStringTemplates + * setUp method * * @return void */ - public function testInitStringTemplates() { + public function setUp() { + parent::setUp(); $templates = [ 'text' => '

{{text}}

', ]; $this->_initStringTemplates($templates); + } + +/** + * testInitStringTemplates + * + * @return void + */ + public function testInitStringTemplates() { $result = $this->templates(null); $this->assertEquals($result, [ 'text' => '

{{text}}

' ]); } +/** + * testFormatStringTemplate + * + * @return void + */ + public function testFormatStringTemplate() { + $result = $this->_formatStringTemplate('text', [ + 'text' => 'CakePHP' + ]); + $this->assertEquals($result, '

CakePHP

'); + } + } diff --git a/Cake/View/Helper/StringTemplateTrait.php b/Cake/View/Helper/StringTemplateTrait.php index ff4f0453f54..d163be84908 100644 --- a/Cake/View/Helper/StringTemplateTrait.php +++ b/Cake/View/Helper/StringTemplateTrait.php @@ -16,6 +16,10 @@ use Cake\View\StringTemplate; +/** + * Adds string template functionality to any class by providing methods to + * load and parse string templates. + */ trait StringTemplateTrait { /** @@ -29,10 +33,11 @@ trait StringTemplateTrait { * Initializes the StringTemplate class and loads templates * * @param array $templates + * @param string $templateClass Class name of the template class to instantiate * @return void */ - protected function _initStringTemplates($templates = []) { - $this->_templater = new StringTemplate(); + protected function _initStringTemplates($templates = [], $templateClass = '\Cake\View\StringTemplate') { + $this->_templater = new $templateClass(); $this->_templater->add($templates); if (isset($this->settings['templates'])) { $this->_templater->load($this->settings['templates']); @@ -53,4 +58,15 @@ public function templates($templates = null) { return $this->_templater->add($templates); } +/** + * Format a template string with $data + * + * @param string $name The template name. + * @param array $data The data to insert. + * @return string + */ + protected function _formatStringTemplate($name, $data) { + return $this->_templater->format($name, $data); + } + }