PHP 无脂“设置”带闭合

发布于 2024-10-18 03:49:05 字数 457 浏览 4 评论 0原文

有谁知道如何使用 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)}

但似乎不支持闭包...如果我使用 require/include 加载模型,请定义函数 w/o F3::set,并在视图中启用用户定义的函数,我可以使其工作。但我希望保持使用 F3::call/F3::set 提供的分离级别。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日裸衫吸 2024-10-25 03:49:05

也许这不是您想听到的答案,但是:F3 的模板引擎限制非常严格,所以我建议根本不要使用它。 F3 本身适用于简单的项目,幸运的是您可以选择要使用的组件。简单的 PHP 模板仍然胜过任何模板引擎,并且通过一个小包装器,您可以轻松访问其中的 F3 变量。然后你的模板可能看起来像这样:

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

包装器只需要包含模板并适当地实现 __get 和 __call 。

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.

微暖i 2024-10-25 03:49:05

好的,1.4.4 版本不支持此功能,但 2.0 版本支持。感谢 2.0 的精彩更新!这是匿名函数支持:

控制器-

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

模板-

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

这是对象支持:

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

{{@myvar->phrase}}

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文