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

block content in main layout for multi module app

Hi I am trying to override the main layout in the multi module skeleton it is something like this common layouts main.volt frontend controller model view index.volt

Inside main.volt I do something like this: <!DOCTYPE html> <html> <head> <title>Common Layout</title> </head> <body> <div style="background: coral"> {% block content %}

        {% endblock %}
    </div>
</body>

</html>

and in my index.volt

{% extends "main.volt" %}

{% block content %} <h1>This is the frontend2!</h1>

<p>You're now flying with Phalcon.</p> {% endblock %}

my Module.php, has this code inside:

    /**
     * Setting up the view component
     */
    $di['view'] = function() {
        $view = new \Phalcon\Mvc\View();

        $view->setViewsDir(__DIR__ . '/views/');
        $view->setLayoutsDir('../../common/layouts/');
        $view->setTemplateAfter('main');
        $view->registerEngines([
                ".volt" => "Phalcon\Mvc\View\Engine\Volt"
        ]);
        return $view;
    };

But nothing is displaying on the main page, Have anyone got this working?



2.1k

it does work. you just have up/down level enough times.

-mod1 --view ---controller ----extended.volt

-mod2 --view ---layout ----base.volt

so basically it would be.

../../mod2/view/layout/base.volt

  • module1
    • controller
    • models
    • views: index.volt
    • Module.php
  • module2
    • controllers
    • models
    • views : index.volt
    • Module.php
  • common
    • layouts: base.volt

the code inside every module.php, set de views path to their owns views folders, but the template path to this common/layouts/base.volt, that is outside of any module...

for the module.php: please take a look at setViewsDir, and setLayoutsDir

    /**
     * Setting up the view component
     */
    $di['view'] = function() {
        $view = new \Phalcon\Mvc\View();

        $view->setViewsDir(__DIR__ . '/views/');
        $view->setLayoutsDir('../../common/layouts/');
        $view->setTemplateAfter('main');
        $view->registerEngines([
                ".volt" => "Phalcon\Mvc\View\Engine\Volt"
        ]);
        return $view;
    };

if I use {{ content }} instead of {% block content %} {% endblock %} works fine, but I want to use the second option... for example if I take index.volt from my module 1 and I do something like this:

    {% extends "../../common/layouts/base.volt" %}
     {% block content %} 
     <h1>Hello world! </h1>
     {% endblock %}

It doesn't do anything and it seems that is trying to look for this "base.volt" inside it's own (Module)

C:\xampp\htdocs\phalcon\app\module1\views/../../common/layouts/base.volt

instead of

C:\xampp\htdocs\phalcon\app\common/layouts/base.volt



2.1k
edited Jan '15

Here is how i set up my view

public index.php

 require __DIR__ . '/../config/services.php';

Global Services

$di->set('view', function () use (&$config) {
    $view = new View();
    $view->setPartialsDir('partials/');
    $view->registerEngines(array(
            '.volt' => function ($view, $di) use (&$config) {
                $volt = new Volt($view, $di);
                $volt->setOptions(array(
                        'compiledPath' => APPLICATION_PATH."/../cache/",
                        'compiledSeparator' => '_',
                        'compileAlways' => true

                ));
                $volt->getCompiler()->addFunction('_',function($resolvedArgs, $exprArgs) {
                    return '$this->translate->_(' . $resolvedArgs . ')';
                });
                $volt->getCompiler()->addFunction('df', function($resolvedArgs, $exprArgs) {
                    return '$'.$exprArgs[0]['expr']['value'].'->{$'.$exprArgs[1]['expr']['value'].'}';
                });

                    return $volt;
            },
            '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
                    ));

    return $view;
},true);

in indidivudual module

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

Ok thanks, let me try your code...really appreciate your help