加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

CodeIgniter集成smarty的方法详解

发布时间:2020-05-23 13:30:32 所属栏目:PHP 来源:互联网
导读:这篇文章主要介绍了CodeIgniter集成smarty的方法,结合实例形式详细分析了CodeIgniter集成Smarty的具体步骤与相关注意事项,需要的朋友可以参考下

本文实例讲述了CodeIgniter集成smarty的方法。分享给大家供大家参考,具体步骤如下:

1.下载smarty

解压到ci的libraries目录 如:

ci/application/libraries/Smarty-2.6.20

2.编写Mysmarty.php 自己的类库文件

代码如下:

Smarty(); $config =& get_config(); // absolute path prevents "template not found" errors $this->template_dir = (!empty($config['smarty_template_dir']) ? $config['smarty_template_dir'] : BASEPATH . 'application/views/'); $this->compile_dir = (!empty($config['smarty_compile_dir']) ? $config['smarty_compile_dir'] : BASEPATH . 'cache/'); //use CI's cache folder if (function_exists('site_url')) { // URL helper required $this->assign("site_url",site_url()); // so we can get the full path to CI easily } } /** * @param $resource_name string * @param $params array holds params that will be passed to the template * @desc loads the template */ function view($resource_name,$params = array()) { if (strpos($resource_name,'.') === false) { $resource_name .= '.html'; } if (is_array($params) && count($params)) { foreach ($params as $key => $value) { $this->assign($key,$value); } } // check if the template file exists. if (!is_file($this->template_dir . $resource_name)) { show_error("template: [$resource_name] cannot be found."); } return parent::display($resource_name); } } // END class smarty_library ?>

3.在autoload.php让ci自动加载smarty

或者 使用模板时再自己加载smarty

load->library("mysmarty");

4.smarty变量赋值 display模板

mysmarty->assign('test','Hello World.'); $this->mysmarty->view('smarty');

注:images css 等外部资源文件 放在ci系统文件夹外 网站根目录下

最好用:

load->helper('url');

base_url()来访问:

不要放到system里

补充:

另外,由于php属于C语言风格,因此下面这款工具同样可以实现php代码的格式化:

C语言风格/HTML/CSS/json代码格式化美化工具:

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》及《》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读