PHP 程式設計/Smarty 模板系統/簡單教程
外觀
< PHP 程式設計 | Smarty 模板系統
- 在你的 Web 伺服器上建立一個名為 Website 的目錄。
- 將 Smarty 的 libs 目錄複製到安裝目錄中。
- 建立一個名為 compile 的目錄。
- 建立一個名為 templates 的目錄。
- 在 Website 目錄中,建立一個名為 index.php 和 Web.class.php 的檔案。確保它們為空。
- Web.class.php 應該像這樣
<?php class Web { function db_connect($db_host,$db_user,$db_pass,$db_db) { $this->link=@mysql_connect($db_host,$db_user,$db_pass) or die("Can't connect to database"); @mysql_select_db($this->link,$db_db) or die("Connected, but can't select the database"); } function db_query($sql) { return @mysql_query($this->link,$sql); } function db_close() { mysql_close($this->link); } } ?>
- index.php 應該像這樣
<?php error_reporting(E_ALL); $db=array("host"=>"localhost","user"=>"root","pass"=>"","db"=>"database"); $tables['content']="test_content"; require_once("Web.class.php"); $web=new Web(); $web->db_connect($db['host'],$db['user'],$db['pass'],$db['db']); require_once("libs/Smarty.inc.php"); $smarty=new Smarty(); $smarty->template_dir="template"; $smarty->compile_dir="compile"; if ( isset($_GET['content_id']) && is_numeric($_GET['content_id']) ) { $sql="SELECT * FROM {$tables['content']} WHERE content_id = '{$_GET['content_id']}' LIMIT 1"; $result=$web->db_query($sql); $rows=array(); while ( $row=mysql_fetch_assoc($result) ) { $rows[]=$row; } if ( count($rows) == 1 ) { $smarty->assign("content_found",true); $smarty->assign("content_content",$rows['0']); } else { $smarty->assign("content_found",false); } $smarty->assign("section","content"); } else { $sql="SELECT content_title,content_date,content_position,content_id FROM {$tables['content']} ORDER by content_position asc"; $result=$web->db_query($sql); $rows=array(); while ( $row=mysql_fetch_assoc($result) ) { $rows[]=$row; } $smarty->assign("section","home"); $smarty->assign("content_content",$rows); } $smarty->display("index.tpl"); $web->db_close(); ?>
- 轉到 templates 目錄
- 建立一個名為 index.tpl 的新檔案,確保它為空
- 建立你自己的 html 設計或任何東西,並在中間(你想放置內容的地方),寫下這些
{if $section == "home"} <ul> {foreach from="content_content" item="content_item"} <li><a href="./?content_id={$content_item.content_id}">{$content_item.content_title}</a></li> {/foreach} </ul> {elseif $section == "content"} <div><h1>{$content_content.content_title}</h1></div> <div>{$content_content.content_content}</div> {else} Sorry, there is no such page here! {/if}
- 建立一個新的 MySQL 表,包含以下資訊
TABLE NAME: test_content PRIMARY KEY: content_id content_id: INTEGER, EXTRA - AUTO_INCREASE content_title: VARCHAR(255) content_date: DATETIME content_content: TEXT content_position: INTEGER
- 根據需要修改你的 index.php 和 index.tpl(注意 index.php 中的 $db,將其更改為你的設定!
- 現在,使用你的 MySQL 客戶端(phpMyAdmin[1]/MySQL[2] 或其他工具),在你的表中新增新行,包括內容和標題。一開始嘗試新增三行。
- 現在,透過你的 Web 瀏覽器訪問你的 Website 目錄(你可能需要將它上傳到你的 Web 伺服器或在你的電腦上設定一個伺服器) ;)
如果你遇到任何問題,請在 IRC irc://irc.freenode.org/php 上提問或聯絡我。我還沒有測試過這個指令碼,所以你可能會發現一些小錯誤。
- 對於 MySQL 的後續版本,請使用以下程式碼
CREATE TABLE `test_content` ( `content_id` INT(11) NOT NULL AUTO_INCREMENT, `content_title` VARCHAR(255) NOT NULL, `content_date` DATETIME NOT NULL, `content_content` TEXT NOT NULL, `content_position` INT(11) NOT NULL, PRIMARY KEY (`content_id`) ) TYPE = myisam;