简体   繁体   中英

How to set top-level cache directory for Volt compiled templates?

I configured Volt engine in Phalcon in the following way:

// create dependency injector
$di = new Phalcon\DI\FactoryDefault(); 

// configure Volt compiler
$di->set('volt', function($view, $di) {
    $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
    $volt->getCompiler()
            ->addFunction('strtotime', 'strtotime')
            ->addFunction('money_format', 'money_format')
            ->addFunction('slownie', 'Kwota::getInstance()->slownie');
    $volt->setOptions(array(
        'compiledPath' => '../cache/'   // this directory EXISTS
    ));
    return $volt;
});

// configure View for backend actions
$di->set('view', function() {
    $view = new Phalcon\Mvc\View();
    $view->setViewsDir('../app/51/views/');
    $view->registerEngines(['.volt' => 'volt']);
    return $view;
});

// configure View for custom content like E-mails, print-view, etc.
$di->set('simpleView', function() {
    $view = new Phalcon\Mvc\View\Simple();
    $view->setViewsDir('../app/volt/');
    $view->registerEngines(['.volt' => 'volt']);
    return $view;
});

As you can see, compiled .volt.php templates should be saved in ../cache directory but they are generated in the same folder where .volt templates are located. What is wrong?

BTW is it safe to use shared (the same) "volt" component for multiple View instances as in example above? Notice that Volt constructor takes $view argument.

Edit: you cannot use shared Volt compiler both for view and simpleView because they interfere.

Look this sample

1) Config id for view

$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);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'
            ));
            return $volt;
        },
    '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
return $view;}, true);

2) for you use the functions should create component

use Phalcon\Mvc\User\Component;
class Somefunctions extends Component {
    public function strtotime($val){
        .
        .
        return $result;
    }
}

3) Config id for this component

$di->set('somefunctions', function(){
return new Somefunctions();});

4) And then, you can use the function in volt :

{{ somefunctions.strtotime('val') }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM