smarty 的安裝與試用

關注的緩存自然后端頁面緩存相關,從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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一、MemCache簡介 session MemCache是一個自由、源碼開放、高性能、分布式的分布式內存對象緩存...
    李偉銘MIng閱讀 3,873評論 2 13
  • 轉載自cr180大神DiscuzX2.5完整目錄結構【source程序文件庫】 /source/admincp后臺...
    cndaqiang閱讀 884評論 1 2
  • 滿園的黃花有人羨慕有人愛。 但偏偏等涼了菜, 愛的碎片終散去東風未來! 含著疼翻動相冊恍若依舊在。 但她的全世界已...
    一直以來都很好閱讀 47評論 0 0
  • 今天是在溫哥華電影學院的一樓117室,觀賞了五部北美華人優秀作品展,平均一部時長20分鐘,其中有傳遞真善美的童話風...
    savvyisme閱讀 252評論 0 0
  • 之前,秧秧已經向大家介紹了春天基礎款的部分單品,下面,接著給大家介紹剩下的部分。 Are you ready? 上...
    微笑的秧秧閱讀 2,570評論 21 65