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

marcosh
  • 8,780
  • 5
  • 44
  • 74

2 Answers2

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;
itrascastro
  • 775
  • 6
  • 14
  • 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. – Alessandro Corradini Mar 10 '15 at 16:05
  • 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? – itrascastro Mar 10 '15 at 16:34
  • 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. – Alessandro Corradini Mar 10 '15 at 17:04
  • 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. – itrascastro Mar 10 '15 at 18:34
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

marcosh
  • 8,780
  • 5
  • 44
  • 74
  • 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. – Alessandro Corradini Mar 10 '15 at 18:42
  • 1
    @AlessandroCorradini looking at the documentation, maybe you could try to have a look into this: http://framework.zend.com/manual/current/en/modules/zend.mvc.plugins.html#forward-plugin – marcosh Mar 10 '15 at 20:44