2012年2月1日 星期三

Smarty基本架構觀念

Smarty可將程式與UI輕易分離,但實際要操作還必須花時間了解,畢竟他沒有像.NET幫你做好好的code behind樣子。

三個資料夾
l   Class:放samrty官方的class
l   templates:放置做好的.tpl樣板檔
l   templates_c:編譯的樣板檔

兩個主要檔案

l   .php:程式撰寫
l   .tplhtml碼樣式排版用

範例說明

 test.php

<?php
header("Content-Type: text/html; charset=big-5");  //語系編碼
require_once ('./class/Smarty/Smarty.class.php');  //smartyclass路徑
$smarty = new Smarty();                      //宣告一個smarty使用
$smarty->template_dir = "./templates";         // templates的路徑
$smarty->compile_dir = "./templates_c";       //compilertemplates路徑
$smarty->assign('title','第一個smarty網頁');   //指定在.tpl中要帶的變數
$smarty->assign('text','利用樣板輸出資料');   //指定在.tpl中要帶的變數
$smarty->display('1123_testsmarty.tpl');       //顯示樣板
?>


說明:
$smarty->assign('title','第一個smarty網頁');
l   'title'為樣板變數名稱
l   '第一個smarty網頁'用來替換樣板的變數名稱

test.tpl
<html>
    <head>
        <title>{$title}</title>
        <meta http-equiv="Content-Type" content="text/html; charset=Big5">
    </head>
    <body>
        <p><font color="red">{$text}</font></p>
        <hr>
        I  am pip lin.
    </body>
</html>

 


說明:
紅字大括號是.php的變數要顯示在前端使用

結果呈現:


沒有留言:

張貼留言