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

How to make a default layout for all modules' views?

This is what I am trying to do:

views module1 controller1 action1.tpl controller2 action2.tpl index.tpl module2 controller1 action1.tpl controller2 action2.tpl index.tpl

I want module2 to get the index.tpl from the parent directory automatically. But I cannot do that when I am writing:

public function registerServices($di) {
    // dispatcher here

    $view = $di->get('view');
    $view->setViewsDir(realpath($view->getViewsDir().'/module2'));
    $di->set('view',$view);
 }

because this way, the views directory root is changed to module2 and now it has no idea about the parent. What can I do to achieve that and have a unified layout throughout the application if the module doesn't have its own layout.



16.3k

i am not sure i understand what you meant. but there is

setMainView method on the view object



8.7k
edited Jun '14

The main view is the file name relative to the views directory, here is the directory structure that I am trying to build:

/themes
        /views
                /module1
                        /controller1
                                /action1.volt
                        /controller2
                                /index.volt
                        index.volt
                /module2
                        ...
                /index.volt <-- I want this view to be the main layout


6.9k
edited Nov '14

Try using $view->setMainView("../index.volt");, I believe that should do what you need.