PHP 配置smarty3

环境  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啦







  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值