Skip to content
Permalink
Browse files

Format array data without errors.

If an array ends up in a string template, we should attempt to format
the data gracefully instead of triggering an error. Imploding on ''
seemed like the safest option at hand.

This is necessary to fix issues like #7361
  • Loading branch information...
markstory committed Sep 8, 2015
1 parent 5921b2f commit 0049cf633c6c55f1fdb42943c0e2da7c68c1bbca
Showing with 30 additions and 1 deletion.
  1. +5 −1 src/View/StringTemplate.php
  2. +25 −0 tests/TestCase/View/StringTemplateTest.php
@@ -233,7 +233,11 @@ public function format($name, array $data)
}
$replace = [];
foreach ($placeholders as $placeholder) {
$replace[] = isset($data[$placeholder]) ? $data[$placeholder] : null;
$replacement = isset($data[$placeholder]) ? $data[$placeholder] : null;
if (is_array($replacement)) {
$replacement = implode('', $replacement);
}
$replace[] = $replacement;
}
return vsprintf($template, $replace);
}
@@ -103,6 +103,31 @@ public function testFormat()
$this->assertEquals('<a href="/">example</a>', $result);
}
/**
* Formatting array data should not trigger errors.
*
* @return void
*/
public function testFormatArrayData()
{
$templates = [
'link' => '<a href="{{url}}">{{text}}</a>'
];
$this->template->add($templates);
$result = $this->template->format('link', [
'url' => '/',
'text' => ['example', 'text']
]);
$this->assertEquals('<a href="/">exampletext</a>', $result);
$result = $this->template->format('link', [
'url' => '/',
'text' => ['key' => 'example', 'text']
]);
$this->assertEquals('<a href="/">exampletext</a>', $result);
}
/**
* Test loading templates files in the app.
*

0 comments on commit 0049cf6

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