Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.



13.7k
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']) . ")";
});


18.3k

It did the job.

Thank you !

Try add function like this:

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