Skip to content
Permalink
Browse files

Make StringTemplateTrait accept either a string or an array.

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 5847081cdd5be7289f76e91052758ddbad9b719d
Showing with 26 additions and 1 deletion.
  1. +7 −1 src/View/Helper/StringTemplateTrait.php
  2. +19 −0 tests/TestCase/View/Helper/StringTemplateTraitTest.php
@@ -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']);
}
}
/**
@@ -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
*

0 comments on commit 5847081

Please sign in to comment.
You can’t perform that action at this time.