I would like to try to use addFunction
method of Phalcon\Mvc\View\Engine\Volt
in controllers for some actions.
I tried to do this but I couldn't exactly achieve what I wanted.
// Setting up view component
$di->set("view", function() use ($config, $di) {
// Initiate service
$view = new View();
// Set views directory
$view->setViewsDir("../apps/backend/views/");
// Return service
return $view;
},true);
// Setting up volt
$di->set("volt", function() use($config, $di) {
$volt = new Volt($di->get("view"), $di);
$volt->setOptions(array(
"compiledPath" => "../vars/volt/",
"compiledSeparator" => '_',
"compiledExtension" => ".php"
));
return $volt;
}, true);
// Register volt engine
$di->get("view")->registerEngines(array(
".volt" => $di->get("volt")
));
Everything seems OK (view files are rendered) with the code above except when I would like to get an output of a view file.
$output = $this->view->getRender("controllerName", "actionName", array("viewVariable" => $myViewVariable), function($view){
$view->setRenderLevel(View::LEVEL_LAYOUT);
});
This returns string(0) ""
If I change my setup to this, I can get output of view file and all other view files of controller / actions are rendered correctly.
// Setting up the view component
$di->set("view", function() use ($config, $di) {
// Initiate service
$view = new View();
// Set views directory
$view->setViewsDir("../apps/backend/view/");
// Set template engine
$view->registerEngines(array(
".volt" => function($view, $di, $config){
$volt = new Volt($view, $di);
$volt->setOptions(array(
"compiledPath" => "../vars/volt/",
"compiledSeparator" => '_',
"compiledExtension" => ".php"
));
return $volt;
},
".phtml" => "\Phalcon\Mvc\View\Engine\Php"
));
}, true);
Unfortunately the code above doesn't allow me to acess Volt through DI
I also tried to set volt while setting view component but it the same problem occured.
// Setting up the view component
$di->set("view", function() use ($config, $di) {
// Initiate service
$view = new View();
// Set views directory
$view->setViewsDir("../apps/backend/view/");
// Set template engine
// Volt
$volt = new Volt($view, $di);
$volt->setOptions(array(
"compiledPath" => "../vars/volt/",
"compiledSeparator" => '_',
"compiledExtension" => ".php"
));
$view->registerEngines(array(
".volt" => $volt,
".phtml" => "\Phalcon\Mvc\View\Engine\Php"
));
// Add to DI
$di->set("volt", $volt, true);
// Return service
return $view;
},true);
I'm trying to add Volt to DI so I can do this in a controller method (action)
// Get volt template engine
$volt = $this->getDI()->get("volt");
$volt->getCompiler()->addFunction(
"customFunction",
function($name) {
return "Hello {$name}";
}
);
Am I doing something wrong here? Why can't I use getRender()
?