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.

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



60.9k

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



60.9k
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'
));


33.6k
Accepted
answer
edited Sep '15

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



60.9k

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



60.9k
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(); ?>