Skip to content

Commit

Permalink
Make StringTemplateTrait accept either a string or an array.
Browse files Browse the repository at this point in the history
An array of templates or string filename should be accepted parameters.
This makes it easier for helpers including this trait to be more
flexible in how their configuration is provided.
  • Loading branch information
markstory committed Feb 10, 2014
1 parent c1df616 commit 5847081
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/View/Helper/StringTemplateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ trait StringTemplateTrait {
*/
public function initStringTemplates($templates = [], $templateClass = '\Cake\View\StringTemplate') {
$this->_templater = new $templateClass($templates);
if (isset($this->settings['templates'])) {
if (empty($this->settings['templates'])) {
return;
}
if (is_string($this->settings['templates'])) {
$this->_templater->load($this->settings['templates']);
}
if (is_array($this->settings['templates'])) {
$this->_templater->add($this->settings['templates']);
}
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/View/Helper/StringTemplateTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ public function testInitStringTemplates() {
]);
}

/**
* test settings['templates']
*
* @return void
*/
public function testInitStringTemplatesArrayForm() {
$this->Template->settings['templates'] = [
'text' => '<p>{{text}}</p>',
];
$this->Template->initStringTemplates();

$result = $this->Template->templates(null);
$this->assertEquals($result, [
'attribute' => '{{name}}="{{value}}"',
'compactAttribute' => '{{name}}="{{value}}"',
'text' => '<p>{{text}}</p>'
]);
}

/**
* testFormatStringTemplate
*
Expand Down

0 comments on commit 5847081

Please sign in to comment.