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 can I pass an object variable to custom function which added to Volt?

I added one function to Volt like this:

$compiler->addFunction('formatPrice', function ($price){
    echo($price); die();
});

In Volt template:

<span class="p-price-number">{{ formatPrice(product.product_price) }}</span>

product.product_price is a string as 100000.

But I received this result on the browser:

$product->product_min_price

What did I do wrong here?



93.7k
Accepted
answer

Try like that:

$compiler->addFunction('formatPrice', function($resolvedArgs, $exprArgs){
    return 'yourFunction(' . $resolvedArgs . ')';
});

Also no need to echo inside function when doing {{ }} in volt