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

Rendering layout with camelCasi directory tree

Hi,

I'm having a problem in a multi-module app, my views are being rendered but not my common layout. I want to have an anonymous layout, a frontend layout and a backend layout.

This is my bootstrap code for configuring the view with Volt support:

    $this->di->set('view', function () use ($config) {
        $view = new View();

        $view->registerEngines(array(
            '.volt' => '\Phalcon\Mvc\View\Engine\Volt',
            '.phtml' => function ($view, $di) use ($config) {
                $volt = new Volt($view, $di);

                # voltCacheDir
                $volt->setOptions(array(
                'compiledPath' => PROJECT_PATH . $config->volt->cacheDir,
                'compiledSeparator' => '_',
                'compileAlways' => true));

                return $volt;
            }
        ));

        return $view;
    });

My application structure is as follows

apps/Project/Application/Signup/View/index/index.phtml apps/Project/Application/Shared/View/index/index.phtml apps/Project/Application/Campaign/View/index/index.phtml

apps/Project/Application/Shared/View/layouts/index.phtml

And my module configuration is here:

apps/Project/Application/Shared/Module.php --> index layout apps/Project/Application/Signup/Module.php --> anoynmous layout apps/Project/Application/Campaign/Module.php --> index or backend layout

I have the following code on every Module.php:

/**
 * Registers the module-only services
 *
 * @param Phalcon\DI $di
 */
public function registerServices(DiInterface $di)
{
    $view = $di->get('view');
    $view->setViewsDir(__DIR__ . '/View/');
    $view->setLayoutsDir(__DIR__ . '/View/layouts/');

    $di->set('view', $view);
}

I've tried to setLayoutsDir directly in my action controller, also in the bootstrap but it doesn't works. Specifying the setLayout() method doesn't works neither. But If I do a getLayouts() it gives me the correct layout but it stills not rendered, so I don't understand what's happening here.

Moreover, I've tried to write my view directory in lowercase or in plural but nothing changes....I would appreciate any help.

thanks!

I had lots of problems with layouts too, and using a relative path instead an absolute one solved the problem. Try

$view->setLayoutsDir('View/layouts/');

How you write your view directory doesn't matter, as long you put the correct path

Hi Stefan,

Thanks to your comment I've found a partial solution.

$view->setLayoutsDir('layouts/');

This line works for me, but I cannot have a common layout because the rest of modules needs the layout directory inside it's view directory.

So...i'm still working on it.

thanks! pablo.

edited Apr '15

you can have a common layout, but you have to put it outside your modules. Define it in bootstrap where you register the view and no longer set it in each module

$view->setLayoutsDir('../../../views/layouts/');

Look also here

https://forum.phalcon.io/discussion/6530/how-to-set-the-main-views-directory#C17640

Exactly, I was just writing the solution. I missed something when I wrote you. This is what I do in my other modules.

    $view = $di->getShared('view');
    $view->setViewsDir(__DIR__ . '/View/');
    $view->setLayoutsDir('../../Shared/View/layouts/');