我在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模板应该保存在../缓存目录中,但是它们是在.volt模板所在的同一个文件夹中生成的。怎么啦?
顺便说一句,对多个视图实例使用共享(相同) "volt“组件安全吗?注意,Volt构造函数采用$view参数。
编辑:您不能为视图和simpleView使用共享Volt编译器,因为它们会产生干扰。
发布于 2016-03-24 03:54:47
看这个样本
1)视图的Config 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)此组件的Config id
$di->set('somefunctions', function(){
return new Somefunctions();});4)然后,您可以使用volt中的函数:
{{ somefunctions.strtotime('val') }}https://stackoverflow.com/questions/36190182
复制相似问题