环境 ubuntu16.04 php7 nginx
首先进入 /var/www/html目录
下载smarty3官方包,解压到当前目录
sudo wget https://github.com/smarty-php/smarty/archive/master.zip
sudo unzip master.zip
然后就可以用了,我们试一下,在/var/www/html下新建一个目录 比如叫 test_smarty
新建四个文件夹
sudo mkdir cache
sudo mkdir configs
sudo mkdir templates
sudo mkdir templates_c
并且将templates和templates_c的权限改为777,不然会导致权限不足显示白屏
sudo chmod 777 templates
sudo chmod 777 templates_c
然后我们增加一个Nginx配置如下
server{
listen 82;
server_name 127.0.0.1 localhost;
access_log /var/www/html/test_smarty/access.log;
error_log /var/www/html/test_smarty/error.log;
root /var/www/html;
location /test_smarty{
index index.php;
root /var/www/html/test_smarty;
try_files $uri $uri/ /test_smarty/index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.(htm|html|gif|jpg|png|js|css)$ {
root /var/www/html;
}
}
然后我们在/var/www/html/test_smarty下新建一个php文件就叫 index.php吧,测试一下
<?php
include("/var/www/html/smarty-master/libs/Smarty.class.php");
$smarty = new Smarty();
$smarty->template_dir = '/var/www/html/test_smarty/templates';
$smarty->compile_dir = '/var/www/html/test_smarty/templates_c';
$smarty->config_dir = '/var/www/html/test_smarty/configs';
$smarty->cache_dir = '/var/www/html/test_smarty/cache';
//$smarty->cache = false;
$test = 'test';
$smarty->assign('test',$test);
$smarty->display("test.html");
?>
然后在templates下就可以写我们的模板html 如下
<html>
<body>
asdasd
{$test}
</body>
</html>
然后打开localhost:82/test_smarty/index.php 就可以看到渲染出来的test啦