Skip to content

Commit

Permalink
Improving the StringTemplateTraitTest and added _formatStringTemplate…
Browse files Browse the repository at this point in the history
…() to the trait.
  • Loading branch information
Florian Krämer committed Dec 23, 2013
1 parent 06a40f6 commit 3f36bd3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
29 changes: 24 additions & 5 deletions Cake/Test/TestCase/View/Helper/StringTemplateTraitTest.php
@@ -1,7 +1,5 @@
<?php
/**
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
Expand All @@ -21,27 +19,48 @@
use Cake\View\Helper\StringTemplate;

/**
* PaginatorHelperTest class
* StringTemplateTraitTest class
*
*/
class StringTemplateTraitTest extends TestCase {

use StringTemplateTrait;

/**
* testInitStringTemplates
* setUp method
*
* @return void
*/
public function testInitStringTemplates() {
public function setUp() {
parent::setUp();
$templates = [
'text' => '<p>{{text}}</p>',
];
$this->_initStringTemplates($templates);
}

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

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

}
20 changes: 18 additions & 2 deletions Cake/View/Helper/StringTemplateTrait.php
Expand Up @@ -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 {

/**
Expand All @@ -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']);
Expand All @@ -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);
}

}

0 comments on commit 3f36bd3

Please sign in to comment.