Issues

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

Issue Type: New Feature Created: 2007-06-19T11:25:58.000+0000 Last Updated: 2008-03-21T16:25:31.000+0000 Status: Resolved Fix version(s): - 1.5.0 (17/Mar/08)

Reporter: Rob Allen (rob) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_View

Related issues: Attachments:

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:

<pre class="highlight">
<?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

<pre class="highlight">
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

Posted by Matthew Weier O'Phinney (matthew) on 2007-11-16T11:00:07.000+0000

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

Posted by Matthew Weier O'Phinney (matthew) on 2007-12-11T15:10:23.000+0000

Partial and PartialLoop view helpers merged to core.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts