Allow an array of variables to be passed to Template::Render()
authorRobert Sesek <rsesek@bluestatic.org>
Mon, 8 Aug 2011 02:17:56 +0000 (22:17 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Mon, 8 Aug 2011 02:17:56 +0000 (22:17 -0400)
testing/tests/views/template_test.php
views/template.php

index dc1e7ce5d3fee971916c75f9806d510ad9c6d727..057b215734146af6731c25534d5c8300d5d7b5d2 100644 (file)
@@ -95,4 +95,15 @@ class TemplateTest extends \PHPUnit_Framework_TestCase
     }
     $this->assertTrue($catch);
   }
+
+  public function testRenderVars()
+  {
+    $template = Template::NewWithData('Some {% $v %}');
+    $this->assertEquals('Some value', $template->Render(array('v' => 'value')));
+
+    $template->v = 'other';
+    $this->assertEquals('Some thing', $template->Render(array('v' => 'thing')));
+
+    $this->assertEquals('Some other', $template->Render());
+  }
 }
index f43e0997f94aa94e935e261a6513e0d021025b85..8814f19c51e4fe544aadd220a502b68d88910ac7 100644 (file)
@@ -75,17 +75,24 @@ class Template
   }
 
   /*! This includes the template and renders it out. */
-  public function Render()
+  public function Render($vars = array())
   {
     $_template = $this->data;
-    $_vars = $this->vars;
+    $_vars = array_merge($this->vars, $vars);
     $render = function () use ($_template, $_vars) {
       extract($_vars);
       eval('?>' . $_template . '<' . '?');
     };
 
     ob_start();
+
+    $error = error_reporting();
+    error_reporting($error & ~E_NOTICE);
+
     $render();
+
+    error_reporting($error);
+
     $data = ob_get_contents();
     ob_end_clean();
     return $data;