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

Problem with implode function added to Volt Compiler

Hi,

I added this function to the Volt compiler like this :

$compiler->addFunction('implode', function($glue, $array) {

    return "implode($glue, $array)";
});

But each time I use it:

{% set numbers = implode(', ', c.Number) %}

{{ numbers }}

This it the result in the compiled file:

<?php $numbers = implode(', ', $c->Number, Array); ?>

<?php echo $numbers; ?>

As you can see there is an extra Array parameter added to the function arguments. This lead to an error of course. Any idea what I'm doing wrong here ?

Thank you.



21.5k
Accepted
answer

Try add function like this:

$compiler->addFunction('implode', function($resolvedArgs, $exprArgs)  use ($compiler) {
    return "implode(" . $compiler->expression($exprArgs[0]['expr']) . ", " . $compiler->expression($exprArgs[1]['expr']) . ")";
});


31.3k

It did the job.

Thank you !

Try add function like this:

$compiler->addFunction('implode', function($resolvedArgs, $exprArgs)  use ($compiler) {
   return "implode(" . $compiler->expression($exprArgs[0]['expr']) . ", " . $compiler->expression($exprArgs[1]['expr']) . ")";
});