0

I have a multi-module application. Each module has it's own view service and folder. But what I need is to use main layout (which is usually views/index.phtml) from a different folder.

I want to render:

  1. render a apps/MODULE-NAME/views/CONTROLLER-NAME/ACTION-NAME.phtml
  2. render a apps/MODULE-NAME/views/layouts/CONTROLLER-NAME.phtml
  3. SKIP rendering a apps/MODULE-NAME/views/index.phtml
  4. render a apps/views/index.phtml

I've tried to use shared layouts from https://github.com/phalcon/mvc/tree/master/multiple-shared-layouts but that is not what I need. In that case layout from MODULE-NAME/views/layouts/CONTROLLER-NAME.phtml does not rendered by application.

pepper
  • 1,732
  • 2
  • 17
  • 33
  • Have you seen the last answer here http://stackoverflow.com/questions/12951048/how-do-i-use-main-layout-views-in-a-multi-module-phalcon-application ? Does the view at level 3 exist for other pages or could you actually delete it entirely? – TommyBs Dec 20 '13 at 20:51

1 Answers1

2

Ok, I'm not sure if this is the right approach, but I think what you can do is something similar to below. Though please consider this means you will have to pick all your views, it won't do any of them automatically

In your Module.php file set-up your view to be a simple view

  $di['view'] = function() {
        $view = new \Phalcon\Mvc\View\Simple();

        $view->setViewsDir(realpath(__DIR__.'../../../apps/'));
        return $view;
};

Then in your bootstrap index.php disable automatic view rendering

   $application->useImplicitView(false); // add this line
   echo $application->handle()->getContent();

Finally in your action methods, you can do the following to pick your views

     public function fooAction(){

echo $this->view->render('common/index');
echo $this->view->render('frontend/Views/layouts/index');
    echo $this->view->render('frontend/Views/Index/foo');


}

No doubt with some thought you could build a plugin or a component that might handle this automatically, but this should achieve what you want (albeit you might have to rework more of your app)

In fact a basic example of the above would be doing something such as the following in a baseController

    public function beforeExecuteRoute($dispatcher){

            $module = $dispatcher->getModuleName();
    $controller = $dispatcher->getControllerName();
    $action = $dispatcher->getActionName();
    $this->temp = $this->view->render('/'.$module.'/Views/'.$controller.'/'.$action);


}

and then in your action for your controller call

     echo $this->temp; //temp set above

You would probably want to check that files exist in the baseController first but obviously you could append different views to $this->temp as well based on the $module/$controller etc.

TommyBs
  • 9,354
  • 4
  • 34
  • 65