备案权重域名预定

 找回密碼
 加入我們

MVC中使用smarty模板

[複製鏈接]
老黑醬 發表於 2021-2-9 09:47:04 | 顯示全部樓層 |閱讀模式


基本思路:

下載smarty源代碼,解壓得到libs文件夾,然後重命名為smarty,存放到項目的libs目錄下。
編寫mySmarty.php,在該文件中完成對smarty模板的配置。
另Controller繼承自mySmarty類,這樣就可以在控制器中方便的使用smarty了。

相關代碼:

mySmarty.php

  1. <?php

  2. /**
  3. * Created by PhpStorm.
  4. * User: koastal
  5. * Date: 2016/5/18
  6. * Time: 11:18
  7. */
  8. require_once __DIR__.'/smarty/Smarty.class.php';
  9. class mySmarty extends Smarty
  10. {
  11.     public function __construct(array $options = array())
  12.     {
  13.         parent::__construct($options);
  14.         $this->left_delimiter = "<{";
  15.         $this->right_delimiter = "}>";
  16.         $this ->setTemplateDir(View_PATH);
  17.         $this->setCacheDir( Lib_PATH."/cache/");
  18.         $this->setCompileDir( Lib_PATH."/compile/");
  19.         $this->setConfigDir( Lib_PATH."/config/");
  20.     }
  21. }
複製代碼


Controller.php

  1. <?php

  2. /**
  3. * Created by PhpStorm.
  4. * User: koastal
  5. * Date: 2016/5/15
  6. * Time: 10:55
  7. */
  8. require_once __DIR__."/mySmarty.php";
  9. class Controller extends mySmarty
  10. {

  11.     function index(){
  12.         return "Controller";
  13.     }
  14.     function __call($name, $arguments)
  15.     {
  16.         exit("function ".$name." doesn't exist.");
  17.     }
  18. }
複製代碼


indexController.class.php

  1. <?php

  2. /**
  3. * Created by PhpStorm.
  4. * User: koastal
  5. * Date: 2016/5/15
  6. * Time: 19:28
  7. */
  8. class indexController extends Controller
  9. {
  10.     function index()
  11.     {
  12.         $this->assign("title","新世界");
  13.         $this->assign("content","大航海時代已經來臨了");
  14.         $this->display("index.html");
  15.     }
  16.     function show(){
  17.         var_dump($_REQUEST);
  18.     }
  19. }
複製代碼


index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title><{$title}></title>
  6. </head>
  7. <body>
  8. view:<h1><{$content}></h1>
  9. </body>
  10. </html>
複製代碼


項目完整代碼地址,點擊這裡

項目裡面沒有包含數據庫訪問的類文件

本帖子中包含更多資源

您需要 登錄 才可以下載或查看,沒有賬號?加入我們

×
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 加入我們

本版積分規則

备案权重域名预定

QQ|4um創業社區

GMT+8, 2024-5-20 11:36

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回復 返回頂部 返回列表