We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Render child action / partial view with model from controller

Hi there, Anybody know how to render partial view with model from controller action or render child action like

{{ render_action(array("controller"=>"index","action"=>"listProduct")) }}

class IndexController{
  public function indexAction(){
      $this->view->products = data_from_db;
  }

  // this action should render as child action
   public function listProductAction(){
      $this->view->products = data_from_db;
  }
}

class CatalogController{
  public function indexAction(){
      // $this->view->products = data_from_db; 
      // no data pass to view that contains the code load partial view
  }
}

Partail view included in view catalog/index does not work if there is no model from controller. I dont want each time including partial view, I have to set model for view from controller.

Could anybody please help me solve this problem?



43.9k

there is no method like the one you're looking for in volt reference

You will have to retrieve all the datas you need in your controller action:

class IndexController extends ControllerBase
{
  public function indexAction(){
    $this->view->products_1 = data_from_db;
    $this->view->products_2 = other_data_from_db;
}

And then display them in your view. If you want to use some kind of reusable "widget", you can use in your index view the "partial" facilities:

<div id="productList">{{ partial("partials/productList", ['products': $products_2]) }}</div>

where "partial" dir is in your view directory



43.9k

otherwise, if you really need different controller actions to be called and displayed in a single view, you can use some ajax trick that will on document ready load in <div id="productList"></div> the result of your listProductAction() and its correspondig view.

   public function listProductAction(){
      $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
      $this->view->products = data_from_db;
  }