smarty 的安装与使用

下载
目前smarty有3.x和2.x版本,两者的对于php的版本要求不同,3.x版要求php5.2+。我下载了2.x版。

在centos根目录下新建data目录
mkdir data
cd datadata目录下下载解压smarty 2.x版
wget https://github.com/smarty-php/smarty/archive/v2.6.28.tar.gz
tar -zxvf v2.6.28.tar.gz

例子

进入smarty的解压目录,把其中的libs目录拷贝到php服务目录下
cd smarty-2.6.28
cp -r libs /var/www/html/libs

在php服务目录下执行以下新建Mysmarty/cache(缓存目录)、Mysmarty/configs(配置目录)、Mysmarty/templates(模板目录)、Mysmarty/templates_c(模板编译目录),注意把cache,templates_c对用户权限修改成可读写
mkdir Mysmarty
cd Mysmarty
mkdir cache configs templates templates_c
chmod 777 cache //更改成全权限,在测试使用中可这样做
chmod 777 templates_c

在templates目录下新建模板文件test.tpl

cd templates
vi test.tpl

编辑test.tpl内容:

<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title><{$title}></title>
 </head>
 <body>
    <{$content}>
 </body>
 </html>

在Mysmarty目录下新建调用文件test.php

cd .. //回到Mysmarty目录
vi test.php

编辑test.php内容:

<?php
include_once("/var/www/html/libs/Smarty.class.php"); //包含smarty类文件
$smarty=new Smarty();  //建立smarty实例对象$smarty
$smarty->template_dir='/var/www/html/Mysmarty/templates';  //设置模板目录
$smarty->compile_dir='/var/www/html/Mysmarty/templates_c'; //设置模板编译生成文件目录
$smarty->config_dir='/var/www/html/Mysmarty/configs';
$smarty->cache_dir='/var/www/html/Mysmarty/cache';  //设置缓存目录
$smarty->cache_lifetime=30;  //缓存时间
$smarty->caching=true;    //true开启缓存,false关闭缓存

$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";

$smarty->assign("title","test");
$smarty->assign("content","hello smarty.");

$smarty->display("test.tpl");
?>

浏览器访问验证
浏览器下访问域名+/Mysmarty/test.php,(自己的的路径) 看到结果就OK了。


添加:
在smarty里面引用css时,要把css文件放到.php相同的文件夹下面
作者:common
链接:https://www.jianshu.com/p/0691b6925b10
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值