Вопрос

Кто-нибудь знает, как назначить, а затем использовать закрытие в модели / представлении, используя F3 :: Set?Или предложить решение следующему сценарию?

Я использую версию 1.4.4

Вот что я пытаюсь сделать:

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

Но закрытие, похоже, не поддерживаются ... Если я загружу модель, используя требуемые / включить, определите функцию W / O F3 :: Set, и включить пользовательские функции в представлении, которые я могу сделать его работу.Но я надеялся сохранить уровень разделения, предоставляемый с помощью F3 :: Call / F3 :: Set.

Спасибо!

Это было полезно?

Решение 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

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top