基本思路:
下載smarty源代碼,解壓得到libs文件夾,然後重命名為smarty,存放到項目的libs目錄下。
編寫mySmarty.php,在該文件中完成對smarty模板的配置。
另Controller繼承自mySmarty類,這樣就可以在控制器中方便的使用smarty了。
相關代碼:
mySmarty.php
- <?php
- /**
- * Created by PhpStorm.
- * User: koastal
- * Date: 2016/5/18
- * Time: 11:18
- */
- require_once __DIR__.'/smarty/Smarty.class.php';
- class mySmarty extends Smarty
- {
- public function __construct(array $options = array())
- {
- parent::__construct($options);
- $this->left_delimiter = "<{";
- $this->right_delimiter = "}>";
- $this ->setTemplateDir(View_PATH);
- $this->setCacheDir( Lib_PATH."/cache/");
- $this->setCompileDir( Lib_PATH."/compile/");
- $this->setConfigDir( Lib_PATH."/config/");
- }
- }
複製代碼
Controller.php
- <?php
- /**
- * Created by PhpStorm.
- * User: koastal
- * Date: 2016/5/15
- * Time: 10:55
- */
- require_once __DIR__."/mySmarty.php";
- class Controller extends mySmarty
- {
- function index(){
- return "Controller";
- }
- function __call($name, $arguments)
- {
- exit("function ".$name." doesn't exist.");
- }
- }
複製代碼
indexController.class.php
- <?php
- /**
- * Created by PhpStorm.
- * User: koastal
- * Date: 2016/5/15
- * Time: 19:28
- */
- class indexController extends Controller
- {
- function index()
- {
- $this->assign("title","新世界");
- $this->assign("content","大航海時代已經來臨了");
- $this->display("index.html");
- }
- function show(){
- var_dump($_REQUEST);
- }
- }
複製代碼
index.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title><{$title}></title>
- </head>
- <body>
- view:<h1><{$content}></h1>
- </body>
- </html>
複製代碼
項目完整代碼地址,點擊這裡。
項目裡面沒有包含數據庫訪問的類文件 |