CodeIgniter + smarty 实现widget功能

王朝学院·作者佚名  2016-05-25
窄屏简体版  字體: 小  |  中  |  大  |  超大  

CodeIgniter + smarty 实现widget功能 2014-02-25 13:09 by 萧 云, ... 阅读, ... 评论, 收藏, 编辑 在开发过程中,经常需要widget功能,一可以隔离页面逻辑,二可以重用代码。结合smarty的plugin功能,可以方便的实现该功能。

譬如,我们的页面中可以这样写:

{{extends file='_layout.html'}} {{block name='content'}}<!--content--><div> <div> {{widgets path='widgets/carousel'}} {{widgets path='widgets/news'}} </div> {{widgets path='widgets/hots'}} {{widgets path='widgets/tops'}}</div><!--/content-->{{/block}}

{{widgets path='widgets/news'}}表示调用widgets Controller 的news action,最终输出的html嵌入这里。

需要实现该功能,可以借助smarty 的function plugin,创建一个function.widgets.php

<?phpfunction smarty_function_widgets($params,&$smarty){ $path = $params['path']; $args = isset($params['args']) ? $params['args'] : NULL; if($path){ $ps = explode('/', $path); $controller = $ps[0]; $method = $ps[1]; require_once APPPATH.'controllers/'.$controller.'.php'; $c = new $controller; if(!isset($params['args'])) $c->$method(); else $c->$method($args); }}?>

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航