smarty是个什么东东哦~~呵呵,曾经被面试官问过,有没有用过smarty当时别说是用啦~我都不知道smarty是什么意思回来上网查了下,这个东东很受追捧它是用PHP写的模版,目的是分离程序员与页面设计人员,也就是使逻辑内容与美工页面设计分开,一方的改变不影响另一方。优点有:
1. 速度:采用smarty编写的程序可以获得最大速度的提高,这一点是相对于其它的模板引擎技术而言的。
2. 编译型:采用smarty编写的程序在运行时要编译成一个非模板技术的PHP文件,这个文件采用了PHP与HTML混合的方式,在下一次访
问模板时将WEB请求直接转换到这个文件中,而不再进行模板重新编译(在源程序没有改动的情况下)
3. 缓存技术:smarty选用的一种缓存技术,它可以将用户最终看到的HTML文件缓存成一个静态的HTML页,当设定smarty的cache属性为
true时,在smarty设定的cachetime期内将用户的WEB请求直接转换到这个静态的HTML文件中来,这相当于调用一个静态的HTML文件。
4. 插件技术:smarty可以自定义插件。插件实际就是一些自定义的函数。
5. 模板中可以使用if/elseif/else/endif。在模板文件使用判断语句可以非常方便的对模板进行格式重排。
不适合使用smarty的地方:
1. 需要实时更新的内容。例如像股票显示,它需要经常对数据进行更新,这类型的程序使用smarty会使模板处理速度变慢。
2. 小项目。小项目因为项目简单而美工与程序员兼于一人的项目,使用smarty会丧失php开发迅速的优点。
smarty的安装与配置:
(1)http://www.smarty.net/下载smarty
(2)将压缩包解压到d:\smarty,这个目录是自己设置的,可以放在任何地方。
(3)修改php.ini配置文件:找到include_path,将其设置成include_path=".;d:\smarty\libs";重启apache or IIS
(4)在服务器目录(apache配置文件里documentRoot下的目录)下建立两个文件:root\smarty\templates和root\smarty\configs
(5)在smarty安装目录下建立两个文件:d:\smarty\templates_c和d:\smarty\cache
下面是一个测试例子:
在服务器目录下建立index.php文件,在root\smarty\templates下建立index.tpl文件:
index.php:
<?php// load Smarty library
require('Smarty.class.php');$smarty = new Smarty;$smarty->template_dir = 'd:/inetpub/root/smarty/templates';
$smarty->config_dir = ' d:/inetpub/root/smarty/configs';
$smarty->cache_dir = 'd:/smarty/cache';
$smarty->compile_dir = 'd:/smarty/templates_c';$smarty->assign('name','fish boy!');$smarty->display('index.tpl');
?>index.tpl
<html>
<body>
Hello, {$name}!
</body>
</html>
运行index.php文件若出现:Hello,fish body!则配置成功。