Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get size of an array in Volt

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.

like image 661
Kvothe Avatar asked Dec 04 '22 02:12

Kvothe


2 Answers

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 }}
like image 107
Raj Avatar answered Jan 06 '23 10:01

Raj


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).

like image 27
Kvothe Avatar answered Jan 06 '23 09:01

Kvothe