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.

Problem with Volt (again)

This morning I just get another problem with Volt. My code is:

$vista->registerEngines([
            '.volt' => 'Phalcon\Mvc\View\Engine\Volt',
            '.phtml' => function ($vista, $ID)
            {
                $volt       = new Volt($vista, $ID);
                $volt->setOptions([
                    'compileAlways' => true,
                    'compiledPath'  => '../vistas/compiladas/',
                    'compiledSeparator' => '_',
                    'compiledExtension' => '.compiled'
                ]);

                return $volt;
            }
        ]);

The problem is that compiledPath only works for one file (a Volt template), but not for the others: so I only get one file in his path, meanwhile I get all the others compiled in their homepaths.

I resolved this problem with a workaround time ago, but this morning just stop working (and that's why I'm trying to do this, more easy, way).



46.9k
edited Dec '14

Try setting the compiledPath as a function. This also has the advantage that you can see what assumptions are being made by logging variables there. Also check that partials, templates and main view all have a trailing slash.

Also check out my Webird project. I've done a lot of work to precompile Volt templates to a cache-static directory so that the webserver is not writing to a directory. In particular you should do something like: $templateFrag = str_replace($phalconDir, "", $templatePath); to create a relative path. Then finish that up with: $voltCompiledPath = "{$voltCacheDir}{$templateFrag}.php" to output to a separate cache directory. Personally I really don't like creating compiled templates in the template source directory.



19.6k

Yeah, I used a function all the time before, and I would try again. But the function isn't reached now (I don't get the echo I put there).

And for your last phrase, don't worry, I separate templates from compiled templates and views from compiled views :D But I do not use a cache-static directory, I use just a directory (don't worry, this app is small to do caching).



46.9k
edited Dec '14

I think that you are perhaps mixing some stuff up.

Set the voltService in the DI like this:

$di->set('voltService', function($view, $di) {
    $volt = new Volt($view, $di);
    $volt->setOptions([
        'compileAlways' => $compileAlways,
        'stat' => $stat,
        'compiledPath' => function($templatePath) use ($variables...) {
            // ...
            return $voltCompiledPath;
        }
    ]);

    return $volt;
});

Wire the view to the viewService (Volt) in its DI function with this very simple code. The extra benefit here is that you can use it for ViewSimple as well.

$view->registerEngines([
    '.volt' => 'voltService'
]);

So seperate your voltService from the part where you wire it up to the view. Having a separate Volt service follows the DI strategy better.



19.6k

I wasn't mixing stuff: you use Volt as a service, while I use it as a closure. It worked all the time. Now I tried (yesterday and today) your piece of code, and yes it works; but Volt options aren't setted (the echo in there doesn't execute) , and the app doesn't work. No error is throwed. This is very very strange.



46.9k

Ok try wrapping everything inside of the closure of DI container in a try/catch block and logging the error. I bet that you will see that you have an exception. Also perhaps turn on full error logging and echo to the screen if you can.



19.6k
Accepted
answer
edited Dec '14

The bootstrap file is in a whole try/catch block (as the tutorials show), but I don't get any exceptions. For security, I try/catched the specific part when setting the view and volt services but nothing. I think I will get back to the previous commit for the sake of god, because this is non-sense.

EDIT: Just as I said, non-sense. It works perfectly know.



46.9k

I found that for some reason exceptions weren't bubbling outside of the DI closure (for some reason) but that was a long time ago and I might have been mistaken.



19.6k

I getted similar not-bubbling 8I think) with ACL few days ago ( http://forum.phalconphp.com/discussion/4070/silent-error-in-acl ). Let's hope that there is no more not-bubbling as you say.



19.6k

Ahhh my friend, I just know what happens: seems to be a bug (?). I thinked that $compiler = $volt->getCompiler() just before $volt->setOptions(..) wasn't dangerous. Indeed, it is, because it just skips the function.

I didn't write it in the post 'cause I didn't think that was the problem, but... So could you explain me why?



46.9k
edited Dec '14

I'm not sure about that but I've encountered a hell of a lot of sharp edges in setting up the Volt compiler for CLI precompiling. I had extreme major time sinks there but I'm happy with the final result. Posting bug reports for that were not a positive experience for me. I'll probably take a look into fixing some of them after Phalcon 2.0 is released as it will be built with Zephir.

I'll add that usually I make things like "options" in this case to need to be invoked on the constructor. Perhaps in this case the Volt object should be continuing to pass the options along to the Volt compiler. Probably the way that it is invoking the compiler is done lazily.