Domanda

Qualcuno sa come assegnare e quindi utilizzare una chiusura in un modello / vista utilizzando F3 :: Set?O offrire una soluzione al seguente scenario?

Sto usando la versione 1.4.4

Ecco cosa sto cercando di fare:

//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)}
.

Ma le chiusure non sembrano essere supportate ... Se carico il modello utilizzando richiedere / includere, definire la funzione w / o f3 :: set e abilitare le funzioni definite dall'utente nella vista che posso farlo funzionare.Ma speravo di mantenere il livello di separazione offerti utilizzando F3 :: CALL / F3 :: Set.

Grazie!

È stato utile?

Soluzione 2

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top