php 模板框架之smarty 的下載和安裝


  • Smarty 官網:

    http://www.smarty.net/
  • Smarty 下載:

    https://github.com/smarty-php/smarty/releases/tag/v3.1.30
    // 下載 tar.gz 放到你的網站服務器中,和 jQuery和 Bootstrap 配合使用。
  • Smarty 使用例程

    // 假設 Smarty 框架已經解壓在 /var/www 目錄下,此目錄是apache2 的家目錄
    //  現在,先寫一個main.php
    // main.php
    <?php
    include "smarty-3.1.30/libs/Smarty.class.php";
    define ("__SITE_ROOT", "/var/www");
    require_once('includes/include.php');
    require_once('includes/functions.php');
    
    $tpl = new Smarty();
    $tpl->template_dir = __SITE_ROOT . "/templates/";    // 指定模板目錄
    $tpl->compile_dir = __SITE_ROOT . "/templates_c/";   // 指定臨時生成的網站目錄
    $tpl->config_dir = __SITE_ROOT . "/configs/";        // 指定配置文件目錄
    $tpl->cache_dir = __SITE_ROOT . "/cache/";           // 指定緩存目錄,這個目錄里面放着最終顯示的網站php 文件
    $tpl->left_delimiter = '<{';
    $tpl->right_delimiter = '}>';
    ?>
    // 創建一些文件夾
    mkdir  templates  templates_c  configs cache
    // 編寫模板
    //  vim templates/test.htm
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=big5">
    <title><{$title}></title>
    </head>
    <body>
    <{$content}>
    </body>
    </html>
    // index.php  調用該模板
    // vim index.php
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    
    <body>
    
    <?php
    require "main.php";
    $tpl->assign("title", "test");
    $tpl->assign("content", "test_1");
    // 上面兩行也可以用這行代替
    // $tpl->assign(array("title" => "測試用的網頁標題", "content" => "測試用的網頁內容"));
    $tpl->display('test.htm');    // 調用模板
    ?>
  • 顯示效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM