PHP 配置smarty3

来源:互联网 发布:java游戏开发视频 编辑:程序博客网 时间:2024/05/21 18:40

环境  ubuntu16.04 php7 nginx


首先进入 /var/www/html目录

下载smarty3官方包,解压到当前目录

sudo wget https://github.com/smarty-php/smarty/archive/master.zipsudo unzip master.zip

然后就可以用了,我们试一下,在/var/www/html下新建一个目录 比如叫 test_smarty


新建四个文件夹 

sudo mkdir cachesudo mkdir configssudo mkdir templatessudo mkdir templates_c

并且将templates和templates_c的权限改为777,不然会导致权限不足显示白屏


sudo chmod 777 templatessudo 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吧,测试一下


<?phpinclude("/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啦







原创粉丝点击