0

I would like to include multiple views within a view in ZF2. I read this link: http://framework.zend.com/manual/current/en/modules/zend.view.quick-start.html

but there is a problem. In this way, I have to pass the values that are within a view, like this:

$secondView = new ViewModel (array('var1' => $var1 ......));

In this mode the controller and second action are bypassed. Is there a way to include a view without bypassing them? I would like variables to be passed from the second action controller, like an include php statement

2 Answers 2

1

If I understood correctly your question, I think that what you're asking is not possible.

My suggestion would be to move the retrieval of the data you need for the secondView somewhere else, away from the controller, and call it both from the second controller and form the first controller to pass them to the secondView.

If you really want to go on with your approach, the only possibility I see is to use javascript and ajax calls to retrieve the partials you need in your view

Sign up to request clarification or add additional context in comments.

2 Comments

I also thought I to include other parts with AJAX. I just tried to do it and works fine, but I would like to do it in pure php.
@AlessandroCorradini looking at the documentation, maybe you could try to have a look into this: framework.zend.com/manual/current/en/modules/…
1

You can use partials for that.

In your module.config.php under the 'view_manager' key you define a template map for your partial:

'view_manager' => array(
    'template_map' => array(
        'myPartial'   => __DIR__ . '/../view/mymodule/partial/myPartial.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),

Then in the view container you can use that partial using the partial() View Helper:

<div><?php echo $this->partial('myPartial', array('var1' => 'value1'); ?></div>

You can also pass variables to your partial. Those variables are referenced in your partial like any other view variable:

echo $var1;

4 Comments

Using partials is similar. Simply, I want render multiple views but I don't want pass other variables, like include method in php. The values of the variables must provide the included action.
I do not understand you. You want to have variables in a parent view and also you want those variables to be available in the child views?
Normally, the view's variables are passed by the its action, ok? If I use '$newView = new ViewModel(); $newView->setTemplate('otherModule/otherController/otherAction')' within an another action, I have to pass the variables relative to the new view. The action relative to the other view, doesn't pass automatically the its variables.
That is the normal behavior. You are only reusing a template. If you need to generate the variables for the second action, maybe that you need is a forward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.