用户
 找回密码
 入住 CI 中国社区
搜索
查看: 10068|回复: 5
收起左侧

CI的视图能用smarty (转贴 英文版)

[复制链接]
发表于 2008-4-6 19:45:32 | 显示全部楼层 |阅读模式
In this article you will learn how to install and use Smarty as a template engine with Code Igniter Framework.

[size=130%]Demo

A downloadable demo (including Smarty-2.6.18) is available from
http://funcs.org/downloads/code-igniter/code-igniter-smarty.zip (~ 200 KB)

update (2007-12-13):  an option added to pass params to mysmarty without calling assign Smarty method.
$this->mysmarty->view('module/func', $params);

http://funcs.org/downloads/code-igniter/code-igniter-smarty-2.zip (~ 200 KB)

Code Igniteris a very good framework but there does have a *native* support for theSmarty template engine, it uses PHP code as template contents.

[size=130%]How to integrate

Things that you should keep in mind (when integrating this example into your projects)

1. You should add 'mysmarty' to autoloaded libraries in application/config/autoload.php

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

2. Copy Mysmarty.php to application/libraries

at the beginning of the file Mysmarty.php you can see the original smarty file structure.
I like this approach because it makes the updates easy, and/or downgrades (if any) in case of buggy new releases.

example
---
require "Smarty-2.6.18/libs/Smarty.class.php";
--


[size=130%]Example Usage of Smarty in Code Igniter:

application/controllers/welcome.php
-----------------------------------

mysmarty->assign('test', 'Hello World.');
$this->mysmarty->view('smarty');
}
}
-----------------------------------
Note:
$this->mysmarty should be available because we are auto loading this library.

view method is used to make an easier transition from code igniter's templates to Smarty ones.
view method also adds a '.tpl' extension if is not specified.


Example Smarty Template: application/views/smarty.tpl
-----------------------------------

{$test}

Def: {$def|default:'n/a'}
-----------------------------------

The file should be accessible via:
http://domain.com/path/to/code-igniter/index.php/welcome/smarty

where path/to/code-igniter is the path to code igniter index.php file.


附件是对应上面对两个文件。

[ 本帖最后由 beautylove 于 2008-4-6 19:48 编辑 ]

code-igniter-smarty-2.zip

210.67 KB, 下载次数: 135

code-igniter-smarty.zip

207.85 KB, 下载次数: 100

 楼主| 发表于 2008-4-6 19:50:49 | 显示全部楼层
http://devcha.blogspot.com/2007/ ... engine-in-code.html

这是原文地址!!  N-人 兄弟的版权意识比较好。。
发表于 2008-4-11 18:16:50 | 显示全部楼层
原帖由 beautylove 于 2008-4-6 19:50 发表
http://devcha.blogspot.com/2007/12/smarty-as-template-engine-in-code.html

这是原文地址!!  N-人 兄弟的版权意识比较好。。


IF了YOU
发表于 2010-7-14 16:28:02 | 显示全部楼层
爬墙去看了下原文。
发表于 2010-8-12 16:27:10 | 显示全部楼层
爬墙去看了下原文。
荒野无灯 发表于 2010-7-14 16:28



    感覺怎麼樣啊?
发表于 2011-10-31 15:42:06 | 显示全部楼层
感谢楼主了

本版积分规则