Get array size in Volt

In Volt (the template engine for Phalcon ), how can I get the number of elements in an array? I tried sizeof , as well as count , length and size (hoping to come across the correct command).

In this particular case, I'm just wondering if there are> 0 elements, but in the future it would be convenient to get the actual number.

+4
source share
2 answers

Length: counts the length of a string or the number of elements in an array or object

MoreInfo: https://docs.phalconphp.com/en/latest/reference/volt.html#filters

 {{ yourarray_Var|length }} 
+9
source

See the accepted answer for the most correct solution to this question. This answer gives an example of how to add a php function to volt though.

While working with 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})"; } ); 

Put this code where you configured your Volt engine (for example, in my services.php file).

+1
source

All Articles