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

Volt doesn't find template

I was setting Volt in my app and I cannot get my first action view 'cause it cannot find the base template. The paths are correct, and in a terminal the file exists within the paths given. It just doesn't makes sense:

index.php

$ID->set('view', function()
    {
        $vista = new View();
        $vista->setViewsDir('../vistas/');
        $vista->registerEngines(
        [
            '.volt'     => 'Phalcon\Mvc\View\Engine\Volt',
            '.phtml'    => function ($vista, $ID)
            {
                $volt = new Volt($vista, $ID);
                $volt->setOptions(
                [
                    'compileAlways' => true,
                    'compiledPath'  => function ($rutaCompilada)
                    {
                        // This function isn't the problem, I tested.
                        $nombres = explode('/', $rutaCompilada);

                        return (
                            '../vistas/compiladas/' .
                            $nombres[count($nombres) - 2] .
                            '-' .
                            $nombres[count($nombres) - 1] .
                            '.compiled'
                        );
                    }
                ]);

                return $volt;
            }
        ]);

        return $vista;
    });

Here is mine:

protected function view()
{
    $config = $this->config;
    $this->di->set('view', function () use ($config) {

        $view = new \Tracksy\ExtendedView();

        $view->setViewsDir(APP_DIR . '/views/');
        $di = new FactoryDefault();

        $view->registerEngines(array(
            '.volt' => function ($view, $di) use ($config) {
                $volt = new VoltEngine($view, $di);

                $volt->setOptions(array(
                    'compiledPath' => APP_DIR . '/cache/volt/',
                    'compiledSeparator' => '_'
                ));

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

        return $view;
    }, true);
}

in your Bootstrap or index.php define a an APP_DIR constant and in setViewsDir try $view->setViewsDir(APP_DIR . '/views/');?



33.8k

I would try that, but this has to be something different. I have the same in other version of my app and it works perfectly, no problems...

edited Nov '14

so if this is in public/index.php you are pointing your view direcotry to ../vistas/ which would mean in your root web directory there is a directory called "vistas" that you want to hold view files, is this correct?



33.8k

Absolutely right (vistas => views). I even used the terminal, doing cd from the public directory, and works. And the file is in the right folder.

The views are in ../vistas/plantillas, so (as in my other versions), in the phtml action view I write {% extends "plantillas/base.volt" %}.

And the exception that I see in the browser is that "../vistas/plantillas/base.volt" does not exists.

"in the phtml action view I write" - sorry if I misinterpreted but should it be a .volt file? Also what is the layout of your ../vistas/plantillas files in this format -> https://pastie.org/9708056?



33.8k

I have a base.volt, that haves <head> and those things. I put a Volt block inside the body, and later, I create the action (index/conexion) like a .phtml, and use the Volt blocks like other normally template engine. That's the way it works in the other version (that is just a copy-paste, nothing else).

And for the layout, I think you refer the directory tree:

aplicacion
    publico
        index.php
    controladores
        IndexController.php
    vistas
        plantillas
            base.volt
        index
            conexion.phtml

But don't get too much in the problem: I'm sure it's something very really stupid, I'm sure. The fact is what is so very really stupid.



33.8k

Ok, the problem was about permissions. But I never had that problem. I set a 777 -R in the app root and know works (and I get back to a secure permission level and still works).