I have a question about using volt-template engine in user components and with simple view.
I wrote a class for user component (extended it form Phalcon\Mvc\User\Component). It has a method to render a simple view:
public function renderPath()
{
$view = $this->getDi()->getView();
$simpleView = new \Phalcon\Mvc\View\Simple;
$simpleView->setViewsDir($view->getViewsDir().'components/');
$render = $simpleView->render($this->key.'/path');
return $render;
}
So, I fetch a current view component to get it's viewsDir and use it as a path for simple view.
This initialization works fine but offers only rendering phtml-files whereas I want to be able to render "*.volt".
If I register engine for volt-template ($simpleView->registerEngines(['.volt' => 'volt']);) then I get an Phalcon\Mvc\View\Exception "A dependency injector container is required to obtain the application services".
However, if change view's DI with setDI by running $simpleView->setDi($this->getDi()); I do not get neither the exception still nor output ($render === null). But in a cache directory I can see that the volt-template is compiled.
To register component I use this code:
$di->set('catalogComponent', function () {
$component = new \ABLib\Components\CatalogComponent;
$component->setCatalogProvider();
return $component;
}, true);
and to render view: {{ this.catalogComponent.renderPath() }}
So, the question is what I'm doing wrong? How can I use volt-template engine inside my component?
Updated:
Managed to solve this problem. Unfortunately, I have no idea why it works. So, in my app I use code like this:
$simpleView = new \Phalcon\Mvc\View\Simple;
$simpleView->setDI($this->getDI());
$simpleView->registerEngines(array(
'.volt' => function ($view, $di) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
'compiledPath' => BASE_PATH.'/cache/',
'prefix' => 'volt_',
));
return $volt;
}
));
And then call $simpleView->render() to render view.
The thing that I can not understand - why it does not work when I add template engine with service. However, when I add a new definition everything starts work.
{{ catalogComponent.renderPath() }}(withoutthis) - that is when using standard volt/view setting. - jodator