Phalcon\Mvc\View\Engine\Volt\Compiler compiler options (not DI Volt view service).

I haven't been able to find any documentation about Phalcon\Mvc\View\Engine\Volt\Compiler compiler options. I need to use Compiler::compileFile but it isn't working for Volt views that reference other views because apparently the compiler does not know which path to load relative to. Is there a way to set this? I tried to pass in TRUE as the third parameter for extends mode but that doesn't seem to help.



42.5k
edited Aug '14

I'm not sure if the stand alone Volt compiler works if the source and destination directory are different.



42.5k
edited Aug '14

In the documentation it does this:

$compiler->compile('views/layouts/main.volt', 'views/layouts/main.volt.php');

So do I need to change the compile working directory to allow relative paths like this or do I need to change the PHP directory? I would like to use absolute paths for both source and destination and this is not working.



42.5k

In the documentation it says "Compiles a template into a file applying the compiler options This method does not return the compiled path if the template was not compiled".

Where are the compiler options documented?

Can you show your implementation of engine and volt ? It could help with resolving your problem.



42.5k
edited Aug '14

This is not the problem but here you go:

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

    // Init volt engine
    $volt = new Volt($view, $di);

    $volt->setOptions([
        'compileAlways' => true,
        'stat' => true,
        'compiledPath' => function($templatePath) use ($config) {
            $cachePath = $config->application->cacheDir;
            $voltPath = "$cachePath/volt";
            if (! file_exists($cachePath)) {
                mkdir($cachePath);
            }
            if (! file_exists($voltPath)) {
                mkdir($voltPath);
            }

            $templateFrag = str_replace($config->application->phalconDir, '', $templatePath);
            $templateFrag = str_replace('/', '%%', $templateFrag);
            $voltCompiledPath = "{$voltPath}/{$templateFrag}-compiled";

            return $voltCompiledPath;
      }
    ]);

    $compiler = $volt->getCompiler();
    require($config->application->phalconDir . '/config/volt_compiler.php');
    return $volt;
});

I need to get the following to work because the compiler doesn't seem to know how to deal with absolute paths. I don't want to write compiled Volt templates to the source folder since it violates the goal of my project.

$compiler->compile($fullPath, $voltCompiledPath);


82.2k

Not the most amazing place to see it, but maybe you want to take a look at the Zephir code: https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/mvc/view/engine/volt/compiler.zep#L2499



42.5k
edited Aug '14

@Phalcon So I can read that strange language ;) but unfortunately those options are not enough for me.

I think that the main problem that I am having is that the CLI standalone compiler doesn't know how to find other Volt templates when an absolute path is given for the source. Should I temporarily change the PHP working directory with PHP call chdir() so that I can use a relative path to inform the compile how to to find Volt includes? I'd much prefer that this functionality was built into the compiler. I really think that it needs a way to change the working directory and in a temporary way so that the compiler could be made to work on different batches of Views in one go without that extra hackiness of creating a new compiler with new base directory.

Also for performance it would be nice if it was possible to tell the never search for the source templates and to only try to read from the compiled templates cache/directory. I would just save an fstat in production.

Also I recently made a NFR issue on github. I think that the Volt DI service should be able to accept a compiler in the constructor. Without this I must do a PHP require to configure the compiler from two different ways of obtaining it.

I really like Volt so far but I think that the compiler bootstrapping is still a bit rough around the edges.