国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php框架 > codeigniter > Codeigniter使用Smarty模板引擎之安装方法

Codeigniter使用Smarty模板引擎之安装方法

来源:程序员人生   发布时间:2014-09-06 08:19:12 阅读次数:7175次

为Codeigniter添加Smarty模板引擎的支持,网上方法都很详细,但是有些注意事项没有提到,这两天闲下来了,把具体步骤和注意事项再理一理。相信有这个需求的人对Codeigniter和Smarty都有一定了解,就对这两个东西不做介绍了。直接进入正题。

一、安装准备

1、Codeigniter,下载地址(下载最新版就好了)

2、Smarty,下载地址(下载最新版就好了)

3、codeigniter-smarty,下载地址 。这是个什么呢?它是一个Smarty的插件,它的作用是让Smarty可以支持Codeigniter,就这么简单。

二、基本原理

将Smarty集成到Codeigniter 中,

  • 首先我们需要安装Codeigniter和Smarty,并且它们能够正常工作,这个毫无疑问。
  • 其次我们需要为Smarty安装"codeigniter-smarty"插件,让Smarty有能力支持Codeigniter。
  • 而后我们需要为Codeigniter创建一个自定义类库,通过类库,我们可以将Smarty和Codeigniter集成到一起去。(这是所有步骤的关键
  • 最后验证安装成功与否。
下面简要说明一下我对这个集成框架模型的理解。如果你没有耐心可以放心的掠过这部分,直接进入下一步,这块是我备忘用的。下图是框架结构的相关部分,深蓝色框为框架部分,而白色框为application部分。这两个框架的集成是在应用层进行的,两个框架本身是不存在耦合的。在应用层通过扩展Codeigniter的类库来引入Smarty模板引擎。在此处,我把Smarty的模板(templates)和模板编译目录(templates_c)全部放入了Codeigniter应用中的views视图中,在不打破Codeigniter的MVC框架的基础上对两个框架进行集成,其实放到哪都可以,不过这个地方要注意模板路径的问题。如果这么集成,Codeigniter的“$this->load->view()”方法和等会会看到的“$this->smarty->view()”的寻址路径是不同的。并且因为路径造成的问题,往往比较隐蔽,不容易发现。我曾在咖啡厅泡了一下午来解决这个问题(当然有点夸张,其实还有别的问题)。Smarty会根据配置的templates_dir寻找模板,而Codeigniter则到application的views中寻找模板,这个需要注意。很显然,上述两个方法在程序中是可以同时存在的,那么应尽量避免同时使用。


三、安装Codeigniter

Codeigniter是一个轻量级的PHP框架,它采用MVC的思想来组织PHP程序。安装很简单,直接将下载的安装包解压后,放到你的服务器目录下即可。具体步骤如下:

1、解压缩Codeigniter安装包,得到如下内容:



2、将这个目录直接上传到你的服务器的根目录下即可,然后访问:http://localhost。如果你看到下边的页面,说明你成功了。


其他说明:如果你希望修改配置等,请参考官方手册:http://codeigniter.org.cn/user_guide/installation/index.html。上述步骤是最简单安装方式,但是如果上边你安装成功了,那么相信其他的问题都可以以此为起点一点点征服。 

四、安装Smarty

Smarty是比较著名的模板引擎,并且功能很强大,带缓存等。Smarty安装也是仅需要两个步骤,解压和上传。

1、将下载的Smarty安装包解压缩,得到下边目录。


2、在Codeigniter的system目录下建立libs/smarty目录


3、将smarty目录中的libs文件夹,上传到新建的Smarty目录下,形成如下目录结构

其他说明:如果你需要更高级的安装,请参看:http://www.smarty.net/docs/zh_CN/installing.smarty.basic.tpl

五、为Smarty安装插件codeigniter-smarty

codeigniter-smarty是一个Smarty插件,它为Smarty提供Codeigniter支持。安装类似于Smarty其他插件的安装,安装和Codeigniter一毛钱关系都没有。分为两步,解压和上传。

1、解压缩codeigniter-smarty,得到如下目录结构:


2、进入codeigniter-smarty/libs/smarty/libs/plugins,然后将所有文件复制到localhost/system/libs/smarty/libs/plugins目录下,就是把codeigniter-smarty中plugins中的东西都复制到smarty的plugins中,这样就完事了。



六、为Codeigniter添加Smarty支持(关键步骤)

1、在application/views目录下新建templates和templates_c两个目录。此处需要注意:如果你使用的是linux等系统,那么注意这两个目录的权限问题。

2、在application/librarys目录下,新建Smarty.php,并写入代码,代码如下:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * Smarty Class * * @package CodeIgniter * @subpackage Libraries * @category Smarty * @author Kepler Gelotte * @link http://www.coolphptools.com/codeigniter-smarty */ //注意这个地方的路径必须能够找到和你的Smarty安装路径一致 require_once( BASEPATH.'libs/smarty/libs/Smarty.class.php' ); class CI_Smarty extends Smarty { function CI_Smarty() { parent::Smarty(); $this->compile_dir = APPPATH . "views/templates_c"; $this->template_dir = APPPATH . "views/templates"; //如下四行是配置config和cache目录,还有模板的起止标志 $this->config_dir = APPPATH . "config"; $this->cache_dir = APPPATH . "cache"; $this->left_delimiter = "<!--{"; $this->right_delimiter = "}-->"; $this->assign( 'APPPATH', APPPATH ); $this->assign( 'BASEPATH', BASEPATH ); log_message('debug', "Smarty Class Initialized"); } function __construct() { parent::__construct(); $this->compile_dir = APPPATH . "views/templates_c"; $this->template_dir = APPPATH . "views/templates"; //如下四行是配置config和cache目录,还有模板的起止标志 $this->config_dir = APPPATH . "config"; $this->cache_dir = APPPATH . "cache"; $this->left_delimiter = "<!--{"; $this->right_delimiter = "}-->"; $this->assign( 'APPPATH', APPPATH ); $this->assign( 'BASEPATH', BASEPATH ); // Assign CodeIgniter object by reference to CI if ( method_exists( $this, 'assignByRef') ) { $ci =& get_instance(); $this->assignByRef("ci", $ci); } log_message('debug', "Smarty Class Initialized"); } /** * Parse a template using the Smarty engine * * This is a convenience method that combines assign() and * display() into one step. * * Values to assign are passed in an associative array of * name => value pairs. * * If the output is to be returned as a string to the caller * instead of being output, pass true as the third parameter. * * @access public * @param string * @param array * @param bool * @return string */ function view($template, $data = array(), $return = FALSE) { foreach ($data as $key => $val) { $this->assign($key, $val); } if ($return == FALSE) { $CI =& get_instance(); if (method_exists( $CI->output, 'set_output' )) { $CI->output->set_output( $this->fetch($template) ); } else { $CI->output->final_output = $this->fetch($template); } return; } else { return $this->fetch($template); } } } // END Smarty Class


上述代码,一方面是将Smarty和Codeigniter进行集成,提供一个公共的类库,以方便调用在Codeigniter中调用Smarty,另一方面其实是对Smarty进行配置(还记得在使用Smarty模板的时候,需要配置那四个路径吗?这里是一样的道理)。

3、让Codeigniter自动加载Smarty类库,方便调用。在codeigniter的config目录下,修改autoload.php中的$autoload['libraries'],自动加载类库smarty,如图:


OK,到此我们已经完成了集成。如果你是按照上边没有改动就完成了的话,那恭喜你,你已经将Smarty和Codeigniter集成到一起了。这样肯定是不放心的,来做一下简单的测试。

七、测试框架

1、测试Smarty框架

(1)、在application中controller目录下建立helloworld.php,并写入代码,代码如下:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Helloworld extends CI_Controller{ function __construct(){ parent::__construct(); } public function index(){ $this->smarty->testInstall();//大家都懂得 } }
(2)、在浏览器输入:http://localhost/helloworld



(3)、测试Smarty框架是否配置成功,如果上边报错,比如找不到路径或者权限问题,那请自行修改,如果你在此处遇到其他问题,也可以给我留言。

2、测试使用Smarty框架

(1)、首先建立模板,在application/views/templates目录下,新建模板文件index.tpl,代码如下:
<h1>Hello,<!--{$name}--></h1>

(2)、然后建立控制器,在application/controller目录下,新建test.php,代码如下:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Test extends CI_Controller{ function __construct(){ parent::__construct(); } public function index($page='index'){ $this->smarty->assign('name', 'forevernull'); $this->smarty->view('index.tpl'); } }
(3)、然后再浏览器输入:http://localhost/test,将看到如下输出:


(4)、OK,大功告成,终于写完了!如果你在上边遇到任何问题,也可以联系我,给我留言是免费的哦!

八、总结

一直觉得挺简单的,写出来真的不容易。其实这两个框架的使用是很灵活的,我只是抛砖引玉,如果你对上述过程熟悉了,你可以更改任何配置。如果你能够读到这里,我真的很佩服你!

九、参考资料

1、CodeIgniter+Smarty - Perfect Together

2、Codeigniter 安装指导

3、Smarty基础安装



生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生