美文网首页
smarty 的安装与试用

smarty 的安装与试用

作者: Addy_Zhou | 来源:发表于2015-09-26 16:29 被阅读125次

关注的缓存自然后端页面缓存相关,从php的smarty的缓存切入,所以就安装用下。

下载

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

例子

  • 进入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,我自己的的路径是http://192.16.137.2/Mysmarty/test.php, 看到结果如下,就OK了。

73VS(}FOA4Y2_A3_RZ2UW45.png

参考

http://www.php100.com/manual/smarty/installing.smarty.basic.html

相关文章

网友评论

      本文标题:smarty 的安装与试用

      本文链接:https://www.haomeiwen.com/subject/uerwcttx.html