初識smarty簡單配置

 

Smarty技術是PHP的精髓所在,隨着PHP版本的逐漸提高,原來的很多方法也許太過時了,下面我就針對最新的PHP5.1.1版本的使用,配置說說如何配置。

下面是一步步的來的,請注意:

1、在官方下載模板庫文件: http://smarty.php.net/download.php

下載了就解壓,看見一個文件夾了,是個 smarty.x.x,打開,裏面有個libs 文件夾,ok,注意這個東西就是我們要的。

2、在你的網站目錄下面,比方我的php網站IIS在物理硬盤d:/web/web/php下面,在這個文件夾下面建立:一個文件夾 test,然後我們把剛提到的 libs文件夾複製test 文件夾下面。{ * 請看本文最後的註釋 TIPS1}

3、在test 文件夾下面再建立4個文件夾:
cache
configs
templates
templates_c

4、建立文件 text.htm:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title><{$title}></title>
</head>
<body>
<{$content}>
</body>
</html>

保存在 templates 目錄下面。

5、然後建立文件模板配置文件: config.php

<?php
    include "libs/Smarty.class.php";
$NowPathArray=explode("test",str_replace("//","/",dirname(__FILE__))) ;
    @define("root_path", $NowPathArray[0]);
    @define('__SITE_ROOT', root_path."test");
    $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/";
    $tpl->left_delimiter = '<{';
    $tpl->right_delimiter = '}>';
?>

保存在主目錄也就是 test下面。

6、在test新建文件test.php文件,輸入:

<?php
    require "config.php";
    $tpl->assign("title", "測試成功了,這是標題");
    $tpl->assign("content", "這是內容");
    $tpl->display('test.htm');
?>

7、在瀏覽器測試test.php顯示爲:

這是內容

恭喜,配置成功.否則,失敗,再檢查是不是按照我說的來的。

Tips1 :爲了能在網站全局使用Smarty技術,我們可以修改PHP.inc裏面的

; Windows: "/path1;/path2"
include_path = ".;c:/php/includes"

改爲:

; Windows: "/path1;/path2"
include_path = ".;c:/php/includes;d:/web/web/php/libs"

使用模板的時候,像前面一樣的方式使用,不要:

include "libs/Smarty.class.php";

直接使用就行了的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章