|
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 编辑 ] |
|