欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

smarty模板引擎從配置文件中獲取數(shù)據(jù)的方法

 更新時(shí)間:2015年01月22日 14:59:47   投稿:shichen2014  
這篇文章主要介紹了smarty模板引擎從配置文件中獲取數(shù)據(jù)的方法,涉及config_load載入配置文件及相關(guān)的讀取技巧,需要的朋友可以參考下

本文實(shí)例講述了smarty模板引擎從配置文件中獲取數(shù)據(jù)的方法。分享給大家供大家參考。具體如下:

當(dāng)某個(gè)變量值,不希望在程序中寫(xiě)死時(shí),就可以把該變量寫(xiě)到配置文件里,并從中獲取(常見(jiàn)的配置樣式)。

第一步:先寫(xiě)一個(gè)配置文件,如數(shù)據(jù)庫(kù)的 db.conf,后綴名conf可以隨便寫(xiě),db.ini也行。文件中內(nèi)容的格式需要固定:key="值",每一行后面不需要添加分號(hào)或者什么,直接回國(guó)換行,如:

配置文件:db.conf

復(fù)制代碼 代碼如下:
host = "localhost"
username = "root"
password = "123456"
db_name = "liuyan"

模板文件:temp.tpl

使用{config_load file="db.conf"} 將文件引入進(jìn)來(lái)。注意,如果寫(xiě)相對(duì)路徑的話,要以訪問(wèn)的頁(yè)面來(lái)看。比如這里,temp.tpl放在templates目錄下,db.conf是放在與templates目錄同一層,但由于瀏覽器訪問(wèn)的文件index.php與db.conf同一層,所以,引用時(shí),直接寫(xiě) {config_load file="db.conf"}  。

復(fù)制代碼 代碼如下:
{config_load file="db.conf"}
<html>
<h2>smarty變量操作,從配置文件中獲取</h2>
<p style="color:green;">{#host#}</p>
<p style="color:red;">{#username#}</p>
</html>

瀏覽器訪問(wèn):index.php

與從php獲取變量數(shù)據(jù)不同,這里不需要用assign分配,而在模板文件里直接加載

復(fù)制代碼 代碼如下:
<?php
//創(chuàng)建smarty對(duì)象
require_once("./libs/Smarty.class.php");
$smarty = new Smarty();
$smarty->display("index.tpl");
?>

希望本文所述對(duì)大家的smarty程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論