Pregunta

¿Alguien sabe cómo asignar y luego usar un cierre en un modelo / vista usando F3 :: Set?U ofrecer una solución al siguiente escenario?

Estoy usando la versión 1.4.4

Aquí está lo que estoy tratando de hacer:

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

Pero los cierres no parecen ser compatibles ... Si carga el modelo usando Requerir / Include, defina la función W / O F3 :: Set, y habilite las funciones definidas por el usuario en la vista. Puedo hacerlo funcionar.Pero esperaba mantener el nivel de separación otorgado usando F3 :: Call / F3 :: Set.

¡Gracias!

¿Fue útil?

Solución 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

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top