4

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.

Kvothe
  • 1,819
  • 2
  • 23
  • 37

2 Answers2

14

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 }}
Raj
  • 1,083
  • 8
  • 22
2

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

Community
  • 1
  • 1
Kvothe
  • 1,819
  • 2
  • 23
  • 37