简体   繁体   中英

PHP Fat Free “set” with Closure

Does anyone know how to assign and then use a closure in a model/view using F3::set? Or offer a solution to the following scenario?

I'm using version 1.4.4

Here's what I'm trying to do:

//In Model - Loaded from controller w/ F3::call
F3::set('getPrice', function($tax, $profile){
//return price
});

//In View - Inside an F3:repeat of @products
{@getPrice(@product.tax, @product.profile)}

But closures don't seem to be supported... If I load the model using require/include, define the function w/o F3::set, and enable user defined functions in the view I can make it work. But I was hoping to maintain the level of separation afforded by using F3::call/F3::set.

Thanks!

Maybe not the answer you'd like to hear but: The template engine of F3 is horrible restrictive, so I'd recommend to not use it at all. F3 itself is okay for simple projects and luckily you can choose which components you want to use. Simple PHP templates still beat any template engine and with a little wrapper you can easily access F3 variables in them. Your template then could look like that:

<?= $this->getPrice($this->product->tax, $this->product->profile) ?>

The wrapper just needs to include the template and implement __get and __call appropiately.

Okay, so version 1.4.4 didn't support this, but version 2.0 does. Thanks for the great updates in 2.0! Here's the anonymous function support:

Controller-

F3::set('func',
    function($a,$b) {
        return $a.', '.$b;
    }
);

Template-

{{@func('hello','world')}}

And here's the object support:

$foo=new stdClass;
$foo->phrase='99 bottles of beer';
F3::set('myvar',$foo);

{{@myvar->phrase}}

http://fatfree.sourceforge.net/page/views-templates

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM