我通过以下方式在Phalcon中配置了Volt引擎:
// 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;
});
如您所见,已编译的.volt.php模板应保存在../cache目录中,但它们是在.volt模板所在的同一文件夹中生成的。有什么问题?
使用共享(相同)"伏特" BTW是否安全?多个View实例的组件,如上例所示?请注意,Volt构造函数接受$ view参数。编辑:您无法对查看和 simpleView 使用共享Volt编译器,因为它们会产生干扰。
答案 0 :(得分:0)
查看此示例
1)视图的配置ID
$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)为你使用的函数应该创建组件
use Phalcon\Mvc\User\Component;
class Somefunctions extends Component {
public function strtotime($val){
.
.
return $result;
}
}
3)此组件的配置ID
$di->set('somefunctions', function(){
return new Somefunctions();});
4)然后,您可以使用伏特中的函数:
{{ somefunctions.strtotime('val') }}