You can use a custom function declared on services for volt too like this:
1 - Create a folder with a friendly name like "Customs" or "Helpers" anything you want
2 - Remeber to declare this on your applications config:
return new \Phalcon\Config(array(
'database' => array(
'adapter' => 'Mysql',
'host' => 'localhost',
'username' => 'root',
'password' => 'your_pass',
'dbname' => 'your_base',
'charset' => 'utf8',
),
'application' => array(
'controllersDir' => __DIR__ . '/../../app/controllers/',
'modelsDir' => __DIR__ . '/../../app/models/',
'migrationsDir' => __DIR__ . '/../../app/migrations/',
'viewsDir' => __DIR__ . '/../../app/views/',
'pluginsDir' => __DIR__ . '/../../app/plugins/',
'libraryDir' => __DIR__ . '/../../app/library/',
'helpersDir' => __DIR__ . '/../../app/Helpers/', //here your folder
'cacheDir' => __DIR__ . '/../../app/cache/',
'vendorDir' => __DIR__ . '/../../vendors/',
'baseUri' => '/clinica_vita/',
)
));
3 - Register this on your loader:
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir,
$config->application->helpersDir,
)
)->register();
4 - Create a class inside this with the name you pick: (Ex: for me is: Filters.php)
class Filters
{
public static formatTree($tree, $parent){
$tree2 = array();
foreach($tree as $i => $item){
if($item['parent_id'] == $parent){
$tree2[$item['id']] = $item;
$tree2[$item['id']]['submenu'] = formatTree($tree, $item['id']);
}
}
return $tree2;
}
}
5 - Inside the services.php call this on the volt object:
$di->set('view', function () use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_ACTION_VIEW);
$view->registerEngines(array(
'.volt' => function ($view, $di) use ($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir . '/volt/',
'compiledSeparator' => '_'
));
$compiler = $volt->getCompiler();
//Aply here the name of your custom function
$compiler->addFunction('formatTree', function($resolvedArgs, $exprArgs) {
return 'Filters::formatTree(' . $resolvedArgs . ')';
});
return $volt;
}
));
return $view;
}, true);
6 - Finally on volt you can call your custom function without model dependencies:
{% set item = formatTree(tree, parent) %}
Just another method to do that without Model way.