Hi,
How can i use in_array()
function on Volt?
I searched on volt doc but i couldnt about that.
I tried in
operator but doesnt work with arrays.
{% if id in array %}
// todo
{% endif %}
Thanks
|
Nov '16 |
7 |
1390 |
0 |
You have to add it in your Service defintion:
$di->set('view', function() use ($di) {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir(__DIR__ . '/views/');
$view->setLayoutsDir('/_layouts/');
$view->setLayout('default');
$view->registerEngines(['.phtml' => function($view, $di) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$options = [
'compiledPath' => $di->getConfig()->site->path->cache . 'volt/backend/',
'compiledExtension' => '.php',
'compileAlways' => $di->getConfig()->debug,
];
$volt->setOptions($options);
// Extended functions
$compiler = $volt->getCompiler();
$compiler->addFunction('in_array', 'in_array');
$compiler->addFunction('is_dir', 'is_dir');
$compiler->addFunction('getimagesize', 'getimagesize');
return $volt;
}]);
return $view;
});
Here is a link with all built in Volt functions. https://docs.phalcon.io/uk/latest/reference/volt.html#functions
To add other functions or create your own refer to the code above.
I'm already using addFunction
but i was expecting direct volt feature ;))
Nice idea also. Works, done it multiple times :)
How about:
{% array[id] is defined %}
Didn't check it though.
{% set myArray = {'Apple', 'Banana', 'Orange'} %}
Doesnt work with that array. My id
is value not index.