zend_form ViewScript decorator / passing arguments
Asked Answered
E

3

5

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

Enliven answered 25/1, 2010 at 19:44 Comment(0)
D
10

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 :)

Darice answered 25/1, 2010 at 20:56 Comment(1)
I personally found the second method you provided to be more effective than the first. Thanks :)Sheikh
E
2

There is another couple of possible ways to achieve adding an icon inside a form, which I think you should consider,

One would be to add your own element type, and add a custom decorator for the icon (I do this to add simple ? icons for help next, or file browsers to form elements). This is a fairly simple affair to do.

The other would be to simply add a HtmlTag decorator to your element, to which you can specify the attributes such as src as options.

These two solutions actually come with another hidden benefit on top of making things simpler to manage, they also remove the usage of the partial view helper, which is used once for every viewscript decorator in use.

The partial view helper can immensely add to the memory and time overhead of your form (which is already fairly large by the way), this is exacerbated when you use the viewscript decorator on elements instead of on the whole form!

Embolden answered 27/4, 2010 at 7:17 Comment(0)
S
2

I needed to pass an html code indicating a score status to the viewscript.phtml file. I found a simpler way than passing it along with the decorator, which is by passing the value from controller's action as if you are passing it to the normal zend view script. e.g:

<Controller>
// pass status and score to view
$this->view->status = $this->_sitenamehelper->get_status($id);
$this->view->score = $this->_sitenamehelper->get_score($id);

<viewscript.phtml partial file>
<?php $form = $this->element; ?>
<b>Status: </b><?php echo $form->getView()->status; ?>
<b>Score: </b><?php echo $form->getView()->score; ?>

And there you can pass any html rendered code .. I guess it would work for icons ..

Mohannad

Steger answered 15/3, 2012 at 14:13 Comment(1)
i find the answer of this question about three days ago. You help me a lot! Thanks! I'm bad English, so sorry.Jampack

© 2022 - 2024 — McMap. All rights reserved.