We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to use Resultset filter in volt

I need user function filter in Resultset of model https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Model_Resultset_Simple.html But i don't know how to write callback function in volt.

Please help me. Thank.



98.9k

Volt does not support anonymous functions, but you can still use PHP there:


<?php
$filtered = $robots->filter(function($robot){
    if ($robot->id < 3) {
        return $robot;
    }
});
?>

{% for robot in filtered %}{{ robot.name }}{% endfor %}

inb4: I would be great that Volt supports anonymous functions

edited Jul '14

Hi,

Thank for help but I can't use this way in my case

In controller i use Paginator

<?php

use Phalcon\Paginator\Adapter\Model as Paginator;

$paginator = new Paginator(array(
        "data" => $cats,
        "limit" => $limit,
        "page" => $numberPage
    ));

?>

And use in views:

{% for index,cat in page.items %} {{ cat.posts.filter({'status=published'}) | length }} {% endfor %}

Thank for help.