Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 5847081cdd5be7289f76e91052758ddbad9b719d 1 parent c1df616
@markstory markstory authored
View
8 src/View/Helper/StringTemplateTrait.php
@@ -38,9 +38,15 @@
*/
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']);
+ }
}
/**
View
19 tests/TestCase/View/Helper/StringTemplateTraitTest.php
@@ -54,6 +54,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
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.