ZF-1587: Allow variables to be passed to render()

Description

It would be useful to be able to pass variables to render() that will then be available to the view script.

e.g. main view script:


<?php 

data 1

$this->render('_table.phtml', array('tableData'=>$this->dataset1);

data 2

$this->render('_table.phtml', array('tableData'=>$this->dataset2); ?>

then, $tableData would be available only within _table.phtml

Suggested Patch


Index: Zend/View.php
===================================================================
--- Zend/View.php   (revision 5379)
+++ Zend/View.php   (working copy)
@@ -43,6 +43,9 @@
      */
     protected function _run()
     {
+        if (is_array(func_get_arg(1))) {
+            extract(func_get_arg(1));
+        }
         include func_get_arg(0);
     }
 }
Index: Zend/View/Abstract.php
===================================================================
--- Zend/View/Abstract.php  (revision 5379)
+++ Zend/View/Abstract.php  (working copy)
@@ -57,6 +57,13 @@
     private $_file = null;
 
     /**
+     * Script variables for file
+     *
+     * @var array
+     */
+    private $_scriptVariables = null;
+
+    /**
      * Instances of helper objects.
      *
      * @var array
@@ -756,16 +763,19 @@
      * Processes a view script and returns the output.
      *
      * @param string $name The script script name to process.
+     * @param array $scriptVariables Additional variables for the script.
      * @return string The script output.
      */
-    public function render($name)
+    public function render($name, $scriptVariables=null)
     {
         // find the script file name using the parent private method
         $this->_file = $this->_script($name);
+        $this->_scriptVariables = $this->_script($scriptVariables);
         unset($name); // remove $name from local scope
+        unset($scriptVariables); // remove $scriptVariables from local scope
 
         ob_start();
-        $this->_run($this->_file); 
+        $this->_run($this->_file, $this->_scriptVariables); 
 
         return $this->_filter(ob_get_clean()); // filter output
     }

Comments

This is implemented via the partial() and partialLoop() helpers of the Zend_View Enhanced proposal, and is available now in the incubator. It will be released as part of 1.1.0

Partial and PartialLoop view helpers merged to core.