Fork me on GitHub

Codeigniter集成smarty

CodeIgniter 是一个小巧但功能强大的 PHP 框架,作为一个简单而“优雅”的工具包,它可以为开发者们建立功能完善的
Web应用程序,smarty的模板机制很强大,一般情况下CI无需整合其他模板标签,因为PHP本身就是一种标签,简单易用。但两者配合起来使用,更加快了开发效率。

codeigniter集成smarty配置如下:

准备代码包

下载codeigniter最新版本:http://codeigniter.org.cn/
下载smarty最新版本:http://www.smarty.net/

配置smarty

在CodeIgniter的application目录下的third_party目录中新建一个名为smarty的目录,将解压出来的libs包复制到该目录中
ci_smarty
ci_smarty

创建模板目录

在application目录的views目录中创建两个文件夹templates、templates_c

编写安装代码

http://www.coolphptools.com/codeigniter-smarty下载代码,Smarty.php(复制至appliction/libraries目录中),并将文件名修改为Ci_smarty.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?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
*/
require_once( APPPATH.'third_party/smarty/libs/Smarty.class.php' );

class CI_Smarty extends Smarty {

function __construct()
{
parent::__construct();

$this->compile_dir = APPPATH . "views/templates_c";
$this->template_dir = APPPATH . "views/templates";
$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

更新CodeIgniter配置

修改application/config/autoload.php文件中的libraries项,让页面自动载入smarty,即:$autoload[‘libraries’]
= array(‘ci_smarty’)。如果不在这里配置,只需在要用到smarty的地方显示调用$this->load->library(‘ci_smarty’);

测试

编写控制器:application/controllers/Example.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
class Example extends CI_Controller {


function index()
{
// This example is taken from the Smarty demo and modified slightly
$this->ci_smarty->assign("Name","Fred Irving Johnathan Bradley Peppergill");
$this->ci_smarty->assign("FirstName",array("John","Mary","James","Henry"));
$this->ci_smarty->assign("LastName",array("Doe","Smith","Johnson","Case"));
$this->ci_smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
array("I", "J", "K", "L"), array("M", "N", "O", "P")));
$this->ci_smarty->assign("contacts", array(array("phone" => "555-1234", "fax" => "555-2345",
"cell" => "999-9999"), array("phone" => "555-4444", "fax" => "555-3333", "cell" => "888-8888")));

$this->ci_smarty->assign("state_values", array( 'AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE',
'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN',
'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA',
'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY' ));
$this->ci_smarty->assign("state_output", array( 'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana',
'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan',
'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma',
'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee',
'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming' ));

// english is the default if you don't set lang
$this->ci_smarty->assign("lang", "english");

// Set the validation rules if this is a submit
if ( $this->input->post('action') == 'submit' )
{
$this->form_validation->set_rules('username', 'Username',
'trim|required|min_length[5]|max_length[12]|xss_clean');
$this->form_validation->set_rules('password', 'Password',
'trim|required|matches[passconf]|md5');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('state', 'State', '');

if ( ! $this->form_validation->run() )
{
$data['error'] = 'Check and fix the form errors below';
}
else
{
$data['message'] = 'Thanks for posting!';
}
}

// These assignments are passed by the associative array
$data['title'] = 'Welcome to the Smarty Website';
$data['bold'] = true;
$data['ip_address'] = $this->input->server('REMOTE_ADDR');

// Calling the convenience function view() that allows passing data
$this->ci_smarty->view( 'example.tpl', $data );
}
}

运行结果:
Example

参考资料
CodeIgniter+Smarty - Perfect Together
代码下载

轻轻的我走了,正如我轻轻的来