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.

Is it possible to disable Volt caching for development?

I don't see a need to compile Volt templates to the file system for development. Is there a way to disable it to have it load directly from memory on each load? I want to do this since it simply makes the setup step more complicated for the environment that I'm creating. I'd simply rather not have to add "make the cache directory writable by the web server". The enviroment that I'm creating will compile SASS, Volt, coffeescript and optionally PHP with Zend, blench (or another) system with Grunt. The environment will be setup to protect as many aspects of the properietary code that people put into it.

Hi, do it in your bootstrap

//index.php
define('DEVELOPMENT', false);
if($_SERVER['REMOTE_ADDR']=="x.x.x.x") // or something else
{
    define('DEVELOPMENT', true);
}

$di->set(’view’, function() {
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir(__DIR__ . ’/app/views/’);
    $view->registerEngines(array(
        ".volt" => function($view, $di) {
            $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
            $volt->setOptions(array(
                ’compiledAlways’ => DEVELOPMENT ? true : false
            ));
            return $volt;
        }
    ));
    return $view;
});


33.7k
edited Apr '14

Hi

You can check file service.php scrow line volt setup

        /**
          * Setting up the view component
          */
          $di->set('view', function() use ($config) {

                        $view = new View();

                        $view->setViewsDir($config->application->viewsDir);

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

                        $volt = new VoltEngine($view, $di);
                        $voltOptions = array(
                        'compiledPath' => $config->application->cacheDir,
                        'compiledSeparator' => '_'
                        );
                        //debug
                        if ('1' != $config->application->debug) {
                                            $voltOptions['compileAlways'] = true;
                                    }
                        $volt->setOptions($voltOptions);

                        //load function php
                        $compiler = $volt->getCompiler();
                        //define variable translate
                        $compiler->addFunction('t', '_');

                        return $volt;
                        }
                        ));

          return $view;
          }, true);


43.9k
edited Apr '14

This compileAlways option does something exactly the opposite of what I want. I want to not have to compile to the filesystem and to have it load completely from memory. I want to do this so that the setup phase is more simple so that the cache directory does not need to be writable by the web server. Then for deployment I will force compile of all templates.



33.7k

compileAlways for volt means no cache



43.9k

It appeared to be creating the cache files with compileAlways set to true.



29.0k
Accepted
answer

so write it to /tmp

//index.php
define('DEVELOPMENT', false);
if($_SERVER['REMOTE_ADDR']=="x.x.x.x") // or something else
{
    define('DEVELOPMENT', true);
}

$di->set('view', function() {
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir(__DIR__ . '/app/views/');
    $view->registerEngines(array(
        ".volt" => function($view, $di) {
            $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
            $volt->setOptions(array(
                'compiledPath'        => "/tmp",
                'prefix'          => md5($_SERVER['SERVER_NAME']),
                'compiledAlways' => DEVELOPMENT ? true : false
            ));
            return $volt;
        }
    ));
    return $view;
});


43.9k

Thanks. I think that this will work fine. I think that I will create a /tmp folder of the md5 of the view path prefixed with 'volt-cache-' and then put the relative path names inside like I'm doing now. I don't want to pollute the /tmp folder as sometimes it is nice to be able to easily look through it. This way it will be possible to look at compiled volt tempaltes if things are going wrong.

change /etc/default/rcS

TMPTIME=1 #tmp files will be cleared every 1 day


43.9k
edited Apr '14
    $volt->setOptions(array(
        'compiledPath' => function($templatePath) use ($config) {

            $hash = md5($config->application->viewsDir);
            $tmpPath = "/tmp/volt-cache-{$hash}";
            if (! file_exists($tmpPath)) {
                mkdir($tmpPath);
            }

            $templateFrag = str_replace($config->application->viewsDir, '', $templatePath);
            $templateFrag = str_replace('/', '%%', $templateFrag);
            $voltCompiledPath = "$tmpPath/$templateFrag";

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

I'm finding the code input in phosphorum to be strange.