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.

How to easily retrieve custom function arguments

Hi all,

I'm writing a custom function to add to my Volt compiler. This function is called like this:

{{ icon("times",["style":"solid"]) }}

The second argument is an array that can have a number of different properties defined. Accessing the first argument is pretty straightforward with

$expr[0]['expr']['value']

However, the second parameter appears to be broken up into a complex abstract syntax tree. Here's the exprArgs:

Array
(
    [0] => Array
        (
            [expr] => Array
                (
                    [type] => 260
                    [value] => pencil
                    [file] => /path/to/file
                    [line] => 88
                )

            [file] => /path/to/file
            [line] => 88
        )

    [1] => Array
        (
            [expr] => Array
                (
                    [type] => 360
                    [left] => Array
                        (
                            [0] => Array
                                (
                                    [expr] => Array
                                        (
                                            [type] => 260
                                            [value] => solid
                                            [file] => /path/to/file
                                            [line] => 88
                                        )

                                    [name] => style
                                    [file] => /path/to/file
                                    [line] => 88
                                )

                        )

                    [file] => /path/to/file
                    [line] => 88
                )

            [file] => /path/to/file
            [line] => 88
        )
)

Now, I can still access that with

$expr[0]['expr']['left']['0']['expr']['value']

but that seems kind of brittle and excessive.

Is there any way to simply get the arguments as they were passed?

I'm running v3.4



5.2k
Accepted
answer

credit goes to Nikolay Mihaylov

$compiler->addFunction(
    'icon',
    function ($resolvedArgs, $exprArgs) {
        return 'Helper::something(' . $resolvedArgs . ')';
    }
);

Helper.php:

<?php

class Helper
{
    public static function something($arg1, $arg2)
    {
        var_dump($arg1, $arg2);
    }
}

I was hoping to avoid setting up an entirely separate class, but that's been the only suggestion.

Thanks for the tip.