Smarty環(huán)境配置與使用入門教程
來源:易賢網(wǎng) 閱讀:690 次 日期:2016-08-23 16:25:28
溫馨提示:易賢網(wǎng)小編為您整理了“Smarty環(huán)境配置與使用入門教程”,方便廣大網(wǎng)友查閱!

本文實例講述了Smarty環(huán)境配置與使用方法。分享給大家供大家參考,具體如下:

下載Smarty(這里以Smarty-2.6.26為例)。解壓下載的文件(目錄結(jié)構(gòu)還蠻復(fù)雜的)。接下來演示給大家一個安裝實例,看過應(yīng)該會舉一反三的。

(1) 在根目錄下建立了新的目錄learn/,再在learn/里建立一個目錄smarty/。將剛才解壓縮出來的目錄的libs/拷貝到smarty/里,再在smarty/里新建templates目錄,templates里新建cache/,templates/,templates_c/, config/。

(2) 新建一個模板文件:index.tpl,將此文件放在learn/smarty/templates/templates目錄下,代碼如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01

<html>

<head>

<metahttp-equiv="Content-Type" content="text/html;charset=gb2312">

<title>Smarty</title></head>

<body>{#$hello#}</body>

</html>

新建index.php,將此文件放在learn/下:

<?php

require 'smarty/libs/Smarty.class.php';

$smarty = new Smarty;//設(shè)置各個目錄的路徑,這里是安裝的重點

$smarty->template_dir ="smarty/templates/templates";

$smarty->compile_dir ="smarty/templates/templates_c";

$smarty->config_dir = "smarty/templates/config";

$smarty->cache_dir ="smarty/templates/cache";

//smarty模板有高速緩存的功能,如果這里是true的話即打開caching,但是會造成網(wǎng)頁不立即更新的問題,當然也可以通過其他的辦法解決

$smarty->caching = false;

$smarty->left_delimiter = "{#"; //重新定義邊界,因為默認邊界“{}“符,在html頁面中嵌入js腳本文件編寫代碼段時使用的就是”{}“符,自定義邊界符還可以是<{ }>, {/ /} 等

$smarty->right_delimiter = "#}";

$hello = "Hello World!";//賦值

$smarty->assign("hello",$hello);//引用模板文件

$smarty->display('index.tpl');?>

(3) 執(zhí)行index.php就能看到Hello World!了。

希望本文所述對大家基于smarty模板的PHP程序設(shè)計有所幫助。

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:Smarty環(huán)境配置與使用入門教程
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)