Kohana 3:如何像WordPress一样在模板/视图中提供API函数?

发布于 2024-09-27 08:51:09 字数 1144 浏览 2 评论 0原文

我正在开发一个项目,该项目允许高级用户定义自己的显示信息和访问一些基本 API 的方式。例如,我提供了一个 show_search_box() 函数,以便用户在想要显示标准搜索框时可以在视图文件中调用该函数,或者可以使用参数调用该函数来自定义搜索表单。

例如,模板中的此代码将显示带有水印文本“在此处输入关键字”的搜索表单。

<div><?php show_search_box('Enter keyword here'); ?></div>

我的想法实际上与 WordPress 在模板标签中所做的完全一样。 (http://codex.wordpress.org/Stepping_Into_Template_Tags

我的想法是创建一个提供所有内容的类这些 API 函数并将该类的对象实例传递给视图文件,以便用户可以在视图中调用 API 函数,如下所示:

<div><?php $API->show_search_box('Enter keyword here'); ?></div>

我认为它会起作用(但尚未测试),但我更喜欢提供一组直接就像 WordPress 一样调用函数。使用 kohana 3 实现此目的的最佳方法是什么?

======更新:我已经测试了传递 $API 对象来查看的方法,并且它按预期工作。

class API {
     public function show_search_box($watermark){....}
}

在控制器中,将 $API 传递给视图/模板,

public function action_index()
{
     $this->template->API = new API();
}

然后如上所述调用视图/模板内的函数。 与那些控制器方法不同,$API 无法访问控制器的变量,除非显式分配它们:例如 $API->setVar('VarName', $a_controller_variable),我认为这非常乏味。

I'm working on a project which allows the advanced user to define their own way of showing the information and access some basic API. For example, I provide a show_search_box() function so that the user can call this function in the view file when they want to show the standard search box, or they could call the function with parameters to customize the search form.

e.g. this code in the template will show a search form with watermark text "Enter keyword here".

<div><?php show_search_box('Enter keyword here'); ?></div>

What I'm thinking actually is exactly like what WordPress does in its template tags. (http://codex.wordpress.org/Stepping_Into_Template_Tags)

My idea is to create a class that provide all those API functions and pass an object instance of the class to the view file, so users can call the API functions in view like:

<div><?php $API->show_search_box('Enter keyword here'); ?></div>

I think it will work, (but have not tested it yet), but I prefer providing a set of direct called functions just like WordPress. What's the best way to do this with kohana 3?

======Update: I have tested the method of pass $API object to view, and it works as expected.

class API {
     public function show_search_box($watermark){....}
}

In the controller, pass the $API to the view/template

public function action_index()
{
     $this->template->API = new API();
}

Then call the function inside view/template as described above.
Unlike those controller methods, $API cannot access the controller's variables unless they're explicitly assigned: e.g. $API->setVar('VarName', $a_controller_variable), which is quite tedious i think.

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

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

发布评论

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

评论(1

枕花眠 2024-10-04 08:51:09

嗯,与 Kohana 2.3 不同,视图不在控制器命名空间中执行,因此您不能简单地执行 $this->something()

如果您将所有功能都放在一个模型中,我们将其称为 API,那么您可以在视图(或基本控制器,如果您希望它随处可用)中执行此操作...

$this->template->internalView = View::factory('your_view')
                                  ->set('API', Model::factory('API));

(假设您有一个 < code> 在父视图中)。

然后您可以在您的视图中执行...

<div><?php $API->show_search_box('Enter keyword here'); ?></div>

这将在您的模型上运行您的方法。视图不应该真正知道模型的存在,但你的情况ma是一个例外。如果您担心破坏 MVC 范例,也许您可​​以使用辅助类而不是模型。

如果您想做 WordPress 所做的事情(有一堆全局函数,我推荐),那么您需要在某处定义它们。 Kohana 并没有一个简单的地方来放置它们,因为它并不能真正满足一堆全局功能。

Well, unlike Kohana 2.3, views don't execute in the controller namespace, so you can't simply do $this->something().

If you have all your functions in one Model, let's call it API, then you could do this in the view (or base controller if you want it available everywhere)...

$this->template->internalView = View::factory('your_view')
                                  ->set('API', Model::factory('API));

(assuming you have a <?php echo $internalView; ?> in a parent view).

Then you could do in your view...

<div><?php $API->show_search_box('Enter keyword here'); ?></div>

Which will run your method on your model. Views shouldn't really know about the existence of models, but your case ma be an exception. Perhaps you could use a helper sort of class instead of a model, if you are worried about breaking the MVC paradigm.

If you want to do what WordPress does (have a bunch of global functions, which I don't recommend), then you will need to define them somewhere. Kohana doesn't really have an easy spot to place them, as it doesn't really cater for a bunch of global functions.

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