作者:小张 | 来源:互联网 | 2023-08-27 10:52
IamcreatingacustomCMSinCodeigniterandIdliketohaveawidgetsystemsimilartowhatisus
I am creating a custom CMS in Codeigniter and I'd like to have a widget system similar to what is used in Wordpress.
我在Codeigniter中创建了一个自定义CMS,我希望有一个类似于Wordpress中使用的小部件系统。
For example, I'd like to have a widget that shows the last 5 posts displayed on the sidebar. I'd also like to be able to control what pages this widget appears on page-by-page basis.
例如,我想要一个小部件,显示侧栏上显示的最后5个帖子。我还希望能够控制这个小部件逐页显示的页面。
I am using Phil Sturgeon's Template library, so an example controller looks like:
我正在使用Phil Sturgeon的模板库,因此示例控制器如下所示:
$this->template->set_partial('header', 'layouts/header');
$this->template->set_partial('footer', 'layouts/footer');
$this->template->set_partial('sidebar', 'layouts/sidebar');
$this->data['title'] = "Create Post";
$this->template->build('create', $this->data);
I'd like to stick with the MVC pattern, so I don't want to put logic in the sidebar view, which is the only thing I can think of right now.
我想坚持使用MVC模式,所以我不想在侧边栏视图中放置逻辑,这是我现在唯一能想到的。
Is HMVC something I should be using for this?
HMVC是我应该用的吗?
How can I tell the sidebar which widgets to display?
如何告诉侧边栏显示哪些小部件?
2 个解决方案
11
Here's Widget library from Wiredesignz
这是Wiredesignz的Widget库
Read more information
阅读更多信息
/**
* Widget Plugin
*
* Install this file as application/plugins/widget_pi.php
*
* @version: 0.21
* $copyright Copyright (c) Wiredesignz 2009-09-07
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
class Widget
{
public $module_path;
function run($file) {
$args = func_get_args();
$module = '';
/* is module in filename? */
if (($pos = strrpos($file, '/')) !== FALSE) {
$module = substr($file, 0, $pos);
$file = substr($file, $pos + 1);
}
list($path, $file) = Modules::find($file, $module, 'widgets/');
if ($path === FALSE) {
$path = APPPATH.'widgets/';
}
Modules::load_file($file, $path);
$file = ucfirst($file);
$widget = new $file();
$widget->module_path = $path;
return call_user_func_array(array($widget, 'run'), array_slice($args, 1));
}
function render($view, $data = array()) {
extract($data);
include $this->module_path.'views/'.$view.EXT;
}
function load($object) {
$this->$object = load_class(ucfirst($object));
}
function __get($var) {
global $CI;
return $CI->$var;
}
}
Example
// application/widgets/Hello_world.php
class Hello_world extends Widget
{
function run() {
$this->render('hello_world');
}
}
In your view call the static “run” method on the widget class:
在您的视图中,在窗口小部件类上调用静态“run”方法:
widget::run('hello_world');
7
What if you want to see some widgets in some controllers? In my opinion, something basic and simple is to keep the widgets of the CMS in a database with the name and a boolean active and inactive.
如果你想在某些控制器中看到一些小部件怎么办?在我看来,基本和简单的东西是将CMS的小部件保存在名称和布尔值为活动和非活动的数据库中。
Finally, in a query, you get an array of widgets. We can extend the main controller and display all the default widgets (using an array, and a global variable)
最后,在查询中,您将获得一组小部件。我们可以扩展主控制器并显示所有默认小部件(使用数组和全局变量)
cms_widget = array(
'msg' => TRUE,
'chat' => TRUE,
'video' => TRUE,
'games' => TRUE
);
}
}
When you call the template of your CMS, you should make a conditional all your widgets to appear in the desired location. For example in column view template:
当您调用CMS的模板时,您应该使所有小部件都有条件显示在所需的位置。例如,在列视图模板中:
if($this->cms_widget['msg']) $this->load->view('widget/msg_view');
if($this->cms_widget['chat']) $this->load->view('widget/chat_view');
if($this->cms_widget['video']) $this->load->view('widget/video_view');
if($this->cms_widget['games']) $this->load->view('widget/games_view');
Now, for example. If you are in sight "Games", will show only the corresponding widget. Suppose I want to see the widget "Games" and "Videos". We would only have to disable the remaining widgets
现在,例如。如果您在“游戏”中,将仅显示相应的小部件。假设我想看到小部件“游戏”和“视频”。我们只需要禁用剩余的小部件
cms_widget = array_merge($this->cms_widget, array(
'msg' => FALSE,
'chat' => FALSE
));
$this->load->view('game_view');
}
}
}