足球游戏_中国足彩网¥体育资讯$

CodeIgniter集成smarty的方法详解
来源:易贤网 阅读:782 次 日期:2016-08-18 16:24:06
温馨提示:易贤网小编为您整理了“CodeIgniter集成smarty的方法详解”,方便广大网友查阅!

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

1.下载smarty

解压到ci的libraries目录 如:

ci/application/libraries/Smarty-2.6.20

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

代码如下:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require "Smarty-2.6.20/libs/Smarty.class.php";

/**

* @file system/application/libraries/Mysmarty.php

*/

class Mysmarty extends Smarty

{

  function Mysmarty()

  {

    $this->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

$autoload['libraries'] = array('database', 'mysmarty');

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

$this->load->library("mysmarty");

4.smarty变量赋值 display模板

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

$this->mysmarty->view('smarty');

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

最好用:

$this->load->helper('url');

base_url()来访问:

base_url()."images/xxx.jpg"

不要放到system里

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

中国足彩网信息请查看网络编程
易贤网手机网站地址:CodeIgniter集成smarty的方法详解
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标