Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Having problem with hierarchical rendering

Hi everybody,

I'm having trouble getting Hierarchical Rendering to work as expected. I have setup my view renderer and passed it to dependecy injector

// public/index.php

$di->set('view', function () {
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir('../app/views/');
        $view->registerEngines(array('.phtml' => '\Phalcon\Mvc\View\Engine\Php'));
        return $view;
    });

I have also created app/views/index.phtml which is my main layout/template and have included the content in that file

<!-- app/views/index.phtml -->
<?php echo $this->getContent() ?>

I have also create controller template and corresponding view files for actions

<!-- app/layouts/user.phtml -->
<?php echo $this->getContent() ?>
<!-- app/user/signin.phtml -->
<h1>SIGN IN</h1>

When executing the signin action of my user controller, only the main layout (app/views/index.phtml) is rendered!

/**
 * @RoutePrefix("/user")
 */
class UserController extends \Phalcon\Mvc\Controller
{
    /**
     * @Get("/signin")
     */
    public function signinAction()
    {

    }
}

What have I missed? Any idea?



7.1k
Accepted
answer
  1. Your base dir is ../app/views/ . Then structure of views can be :
../app/views/
                   Index.phtml
                   User/
                           index.phtml  # controller layout
                  layouts/
                           User.phtml

Then, because Phalcon support PSR-0, all folder is named with uppercase first letter. As result :

../app/Views/
                   Index.phtml
                   User/
                           Index.phtml  # controller layout
                  layouts/
                           User.phtml

and in module

$di->set('view', function () {
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir('../app/Views/');
        $view->registerEngines(array('.phtml' => '\Phalcon\Mvc\View\Engine\Php'));
        return $view;
    });


891

Thanks Oleg,

That did it! Problem solved :)

Hi Oleg,

I'm having a similar issue in a multi-module app, my views are being rendered but not my layout.

This is my bootstrap code for the view:

    $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 (two module example):

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

In my apps/Project/Application/Shared/Module.php or apps/Project/Application/Signup/Module.php I have the following code:

/**
 * 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);
}

After this, views is rendered but not the layout. I would appreciate some help. :-)