Browse Source

Make StringTemplate::format() return null if template not found.

This makes is possible to distinguish error case versus a formatted string
which could be empty.
ADmad 10 years ago
parent
commit
46655805c1
2 changed files with 11 additions and 4 deletions
  1. 3 3
      src/View/StringTemplate.php
  2. 8 1
      tests/TestCase/View/StringTemplateTest.php

+ 3 - 3
src/View/StringTemplate.php

@@ -220,16 +220,16 @@ class StringTemplate
      *
      * @param string $name The template name.
      * @param array $data The data to insert.
-     * @return string
+     * @return string|null Formatted string or null if template not found.
      */
     public function format($name, array $data)
     {
         if (!isset($this->_compiled[$name])) {
-            return '';
+            return null;
         }
         list($template, $placeholders) = $this->_compiled[$name];
         if ($template === null) {
-            return '';
+            return null;
         }
         $replace = [];
         foreach ($placeholders as $placeholder) {

+ 8 - 1
tests/TestCase/View/StringTemplateTest.php

@@ -89,11 +89,18 @@ class StringTemplateTest extends TestCase
     public function testFormat()
     {
         $templates = [
-            'link' => '<a href="{{url}}">{{text}}</a>'
+            'link' => '<a href="{{url}}">{{text}}</a>',
+            'text' => '{{text}}'
         ];
         $this->template->add($templates);
 
         $result = $this->template->format('not there', []);
+        $this->assertNull($result);
+
+        $result = $this->template->format('text', ['text' => '']);
+        $this->assertSame('', $result);
+
+        $result = $this->template->format('text', []);
         $this->assertSame('', $result);
 
         $result = $this->template->format('link', [