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

Shared layout doesnt work for me

Hello,

in my module.php i set

$di['view'] = function(){

        $view = new \Phalcon\Mvc\View();

        $view->setViewsDir(__DIR__ . '/views/');

        $view->setLayoutsDir( '../../common' . DS . 'layouts');
        $view->setLayout('main');

        return $view;
    };

what I want to happen is :

    include :
        - common/layouts/main.phtml ( where i have <head>head etc... )
        - module/views/index.phtml ( where i have "hi " )

but now i see only "hi"

is there anything else I need to configure ? or I should put this configuration elsewhere ?

It wouldn't look up a layout in common/layouts.main.phtml but in common/layouts/main.phtml



85.5k

oops. that was a syntax error in my question, sorry i fix it now.

I will upgrade to 2.1 in 2 hours to test there. i saw you made some changes regarding views, paths and so on...

If it is still not working i will post a comment again



85.5k
edited Sep '15

Ok i tested with 2.1 still the same...

/modules/Home/Module.php

public function registerServices(DiInterface $di)
{

    $config = require __DIR__ . "/config/config.php";

    //Registering a dispatcher
    $di->set('dispatcher', function () {
        $dispatcher = new \Phalcon\Mvc\Dispatcher();
        $dispatcher->setDefaultNamespace("Mods\Home\Controllers\\");
        return $dispatcher;
    });

    $di['view'] = function(){

        $view = new \Phalcon\Mvc\View();

        $view->setViewsDir(__DIR__ . '/views/');

        $view->setLayoutsDir( MODULES_DIR . 'common' . DS . 'layouts' . DS);
        $view->setLayout('main');
        $view->setTemplateBefore('main');
        $view->setTemplateAfter('main');

        return $view;
    };

}

it only renders my /modules/Home/Views/Index.phtml

//edit my router for "/"

$router->add('/', array(
    'module'     => 'home',
    'controller' => 'index',
    'action'     => 'index'
));


34.6k
Accepted
answer
edited Sep '15

Maybe you can attach an events manager to check what paths are being tried: https://docs.phalcon.io/en/latest/reference/views.html#view-events



85.5k

Thx., i will inform you once i got back home. Had to go out for 2 hr.



85.5k
edited Sep '15

ok i managed to make it work, but now view/index.phtml is rendered before my main layout. ...

my common main.phtml

<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <?php
        $this->assets->outputCss();
        $this->assets->outputJs();

    ?>

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

my index.phtml

<h1>Blog Title</h1>

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