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包复制到该目录中
创建模板目录
在application目录的views目录中创建两个文件夹templates、templates_c
编写安装代码
1 |
|
更新CodeIgniter配置
修改application/config/autoload.php文件中的libraries项,让页面自动载入smarty,即:$autoload[‘libraries’]
= array(‘ci_smarty’)。如果不在这里配置,只需在要用到smarty的地方显示调用$this->load->library(‘ci_smarty’);
测试
编写控制器:application/controllers/Example.php1
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
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 );
}
}
运行结果: