Display controller with view in other view

Hi,

I have two controllers

  • IndexController with indexAction() and view/index/index.phtml
  • ExampleController with exampleAction() and view/example/example.phtml

I would like put example.phtml in index.phtml with execute exampleAction().

Currently I use $view->getRender(), but this function not execute controller and render again layout.

Hi @TrueShutDown if you want only show example you must use

    class IndexController extends Controller {

        public function indexAction() {
            return $this->view->pick('example/example');
        }

    }

But if you want show both you must use a partial() in index.phtml

Good luck

so you just need to pick the folders where the index are if you just eant to cha nge te view for that controller ...

class IndexController extends ControllerBase {
        public function indexAction() {
            return $this->view->pick('index/second_index'); // where index is the folder of the controller and the second_index is the .PHTML file inside that folder
        }
    }

or if you want to include an view to you current view you can use :

    <?php 

    echo $this->partial("folder_name/index_name");  //where fodler name is the folder where you need the index and the index_name is the .phtml file

    ?>
edited 7d ago

Thanks for answers,

pick() not working for me, because I have to show a few view in one view. Partial works good, but I must manual execute other controllers in indexController. I thinks this is no flexible solution. No other way?

If you need to render multiple views through another view you could use a layout, let's say views/layouts/common.phtml:

<!--You logic here-->

<?php echo $this->getContent(); ?>

And then use that layout in the actions you need:

class ExampleController extends Controller
{
    public function exampleAction()
    {
        $this->view->setLayout('common');
    }
}

This will bypass the views/layouts/example.phtml layout, if one is present. If this is not desired there is also setTemplateBefore or setTemplateAfter