欢迎来到天天文库
浏览记录
ID:13782384
大小:31.50 KB
页数:4页
时间:2018-07-24
《php smarty快速入门》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、PHP的世界里已经出现了各式各样的模板类,但就功能和速度来说Smarty还是一直处于领先地位,因为Smarty的功能相对强大,所以使用起来比其他一些模板类稍显复杂了一点。现在就用30分钟让您快速入门。 一.安装 首先打开网页http://smarty.php.net/download.php,下载最新版本的Smarty。解压下载的文件(目录结构还蛮复杂的)。接下来我演示给大家一个安装实例,看过应该会举一反三的。 (1)我在根目录下建立了新的目录learn/,再在learn/里建立一个目录smarty/。将刚才解压缩出来的目录的libs/拷贝到smarty/里,再在smarty/里
2、新建templates目录,templates里新建cache/,templates/,templates_c/,config/. (2)新建一个模板文件:index.tpl,将此文件放在learn/smarty/templates/templates目录下,代码如下: 复制代码代码如下:
3、ext/html;charset=gb2312">
4、y/templates/templates_c"; $smarty->config_dir="smarty/templates/config"; $smarty->cache_dir="smarty/templates/cache"; //smarty模板有高速缓存的功能,如果这里是true的话即打开caching,但是会造成网页不立即更新的问题,当然也可以通过其他的办法解决 $smarty->caching=false; $hello="HelloWorld!"; //赋值 $smarty->assign("hello",$hello); //引用模板文件 $smarty->dis
5、play('index.tpl'); ?>(3)执行index.php就能看到HelloWorld!了。 二.赋值 在模板文件中需要替换的值用大括号{}括起来,值的前面还要加$号。例如{$hello}。这里可以是数组,比如{$hello.item1},{$hello.item2}… 而PHP源文件中只需要一个简单的函数assign(var,value)。 简单的例子: *.tpl: Hello,{$exp.name}!Good{$exp.time} *.php: $hello[name]=“Mr.Green”; $hello[time]=”morning”; $smarty->a
6、ssign(“exp”,$hello); output: Hello,Mr.Green!Goodmorning 三.引用 网站中的网页一般header和footer是可以共用的,所以只要在每个tpl中引用它们就可以了。 示例:*.tpl: {includefile="header.tpl"} {*bodyoftemplategoeshere*} {includefile="footer.tpl"} 四.判断 模板文件中可以使用ifelse等判断语句,即可以将一些逻辑程序放在模板里。"eq","ne","neq","gt","lt","lte","le","gte""ge","is
7、even","isodd","isnoteven","isnotodd","not","mod","divby","evenby","oddby","==","!=",">","<","<=",">="这些是if中可以用到的比较。看看就能知道什么意思吧。 示例: {if$nameeq"Fred"} WelcomeSir. {elseif$nameeq"Wilma"} WelcomeMa'am. {else} Welcome,whateveryouare.
此文档下载收益归作者所有