In Volt (the template engine for Phalcon) how can I get the number of elements in an array? I've tried sizeof
and also count
, length
and size
(hoping to stumble upon the correct command).
In this particular instance I'm just interested in whether there are >0 elements but in the future it would be handy to be able to get the actual number.
Length: Counts the string length or how many items are in an array or object
MoreInfo: https://docs.phalconphp.com/en/latest/reference/volt.html#filters
{{ yourarray_Var|length }}
See the accepted answer for the most correct solution to this question. This answer gives an example of how to add a php function into volt though.
Working from the answer to this question, I used the following code to add this function to Volt
.
$volt->getCompiler()->addFunction(
'count',
function($key)
{
return "count({$key})";
}
);
Place this code where you set up your Volt
engine (e.g. in my services.php file).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With