Search code examples
phpzend-frameworkzend-formzend-view

zend_form ViewScript decorator / passing arguments


I have a form that is extend from Zend_Form. I am placing the form into a ViewScript decorator like this:

$this->setDecorators(array(array('ViewScript', array('viewScript' => 'game/forms/game-management.phtml'))));

I'd like to pass in a variable to this ViewScript but am not sure how this could be done.

Since the partial renders out as a Zend_View (allowing $this->app_store_icon for rendering), it seems like there should be a way to pass variables to be rendered. I tried the following but to no avail.

$this->setDecorators(array(array('ViewScript', array('viewScript' => 'game/forms/game-management.phtml'),array('app_store_picon'=>$current_app_store_picon))));

Any help on how to get this done would be appreciated.

thanks


Solution

  • This one's a bit tricky, took me bout a half an hour to figure it out, but it can be done :)

    The point is, that you're passing the options to the ViewScript decorator and not to the element. Adding the option:

    $this->setDecorators(array(array('ViewScript', array(
        'viewScript' => 'test.phtml',
        'foo'=>'baz',
    ))));
    

    or an array of options:

    $this->setDecorators(array(array('ViewScript', array(
        'viewScript' => 'test.phtml',
        array(
            'foo'=>'baz',
            'spam'=>'ham',
        ),
    ))));
    

    Getting that out in the partial, test.phtml:

    $option = $this->element->getDecorator('ViewScript')->getOptions();
    

    In the first case, with one option passed it'll be $option['foo'] and in the second it'll be $option[0]['foo']

    HTH :)