Is it possible to have Volt templates compiled with a filename as a relative path to the volt directory?

I would like to be able to compile Volt templates and to have the file names stored as a relative path in the cache directory. This is necessary to allow a framework to be compiled on the development environment and then deployed to the production server.

Additionally I want to be able to copy a folder say version '1.3.2' to '1.3.3' and to tweak some JS or CSS on a live environment without needing to redeploy from the development environment. This sort of flexibility has come in very handy in the past when large live real data sets cause unexpected errors. It can be useful for tweaking themes with a client on the phone and reloading their browser.

So it seems that at the moment that the way Volt is setup this will not be possible and I'm not excited about having anything that is capable of executing PHP code to be writable by the web server. I suppose that I could make my CLI Site Copy script execute a volt recompile but I'd rather not have to do that step.



46.8k
edited Apr '14

I figured out how to do it. Care needs to be given to ensure that no extra '/' are used in the path. realpath() can be used but this will result in more work for the server.

// Recursively create the same structure in another directory
    $volt->setOptions(array(
        'compiledPath' => function($templatePath) use ($config) {
            $templateFrag = str_replace($config->application->viewsDir, '', $templatePath);
            $templateFrag = str_replace('/', '%%', $templateFrag);
            $voltCompiledPath = $config->application->cacheDir . 'volt/' . $templateFrag;

            return $voltCompiledPath;
        },
            'compiledExtension' => '.volt-compiled'
    ));


46.8k

Damn that turned out ugly.... not sure what I did wrong with pasting it.

edited Apr '14

You don't have to use the default location (which is the folder the view template resides) for your compiled volt templates.

You can set a different folder which is outside your executable folders. In my case I always set it in /var/volt

Example:

$config = $this->di['config'];
$di     = $this->di;

$this->di['volt'] = function ($view, $di) use ($config) {

    $debug = (isset($config->app_debug)) ?
             (bool) $config->app_debug   :
             false;
    $volt  = new PhVolt($view, $di);
    $volt->setOptions(
        [
            'compiledPath'      => $config->app_volt->path,
            'compiledExtension' => $config->app_volt->extension,
            'compiledSeparator' => $config->app_volt->separator,
            'stat'              => $debug,
            'compileAlways'     => $debug,
        ]
    );

    return $volt;
};

On the above you can also incorporate the version as you mention

$config = $this->di['config'];
$di     = $this->di;

$this->di['volt'] = function ($view, $di) use ($config) {

    $debug = (isset($config->app_debug)) ?
             (bool) $config->app_debug   :
             false;
    $volt  = new PhVolt($view, $di);
    $volt->setOptions(
        [
            'compiledPath'      => $config->app_volt->path . '/' . $config->app_version,
            'compiledExtension' => $config->app_volt->extension,
            'compiledSeparator' => $config->app_volt->separator,
            'stat'              => $debug,
            'compileAlways'     => $debug,
        ]
    );

    return $volt;
};


46.8k

More example in this thread. http://forum.phalconphp.com/discussion/1995/is-it-possible-to-disable-volt-caching-for-development-

I'll post a newer version with I have the deployment system setup. It will all be open source though.