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

Phalcon Layouts

Hi

I'm having problem with using layouts.

According to docs -https://docs.phalcon.io/en/latest/reference/views.html - there is hierarchical rendering. So i've used skeleton application which outputs hello world, and created dir Layouts in views, then put there index.phtml. But its content is not rendered. What's more, if i remove index.phml from views dir, nothing is rendered. Any clues?



17.5k

Inside of your app -> views -> layouts directory, create your layout file... In this case, assume layout.phtml.

Inside of your "MyController" controller, make sure you have this:

    public function initialize() {
        $this->view->setTemplateAfter('layout');
    }

This will tell the controller to use the layout template.

Inside of your layout file, make sure you have this where you want the included content to be placed:

    <?php echo $this->getContent(); ?>

Then when you resolve a route, you'll do this:

    $router->addGet("/some-path",[
        'controller' => 'mycontroller',
        'action' => 'somepath'
    ]);

Then inside of your "MyController" controller you'll do this:

    public function somepathAction() {
        $this->view->pick('some-path');
    }

At this point you'll have a file in app->views->some-path.phtml (or volt or whatever) that will have the content you want rendered inside of the layout.