smarty模板引擎如何從php中獲取數(shù)據(jù)-創(chuàng)新互聯(lián)

這篇文章主要介紹“smarty模板引擎如何從php中獲取數(shù)據(jù)”,在日常操作中,相信很多人在smarty模板引擎如何從php中獲取數(shù)據(jù)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”smarty模板引擎如何從php中獲取數(shù)據(jù)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

目前創(chuàng)新互聯(lián)公司已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、京口網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

smarty可以分配($smarty->assign)的變量類型:所有php支持的數(shù)據(jù)類型——基本數(shù)據(jù)類型、復(fù)合數(shù)據(jù)類型、特殊數(shù)據(jù)類型(具體見smarty相關(guān)手冊)。

操作/顯示文件:index.php


復(fù)制代碼 代碼如下:

<?php
//創(chuàng)建smarty對象
require_once("./libs/Smarty.class.php");
$smarty = new Smarty();
$smarty->assign("aa","hello word");//分配字符串
$smarty->assign("bb",123);//分配整型
$smarty->assign("cc",90.8);//分配float型,浮點(diǎn)型
$smarty->assign("dd",true);//分配字符串
//分配數(shù)組,數(shù)組一般從數(shù)據(jù)庫取出,這里直接給數(shù)組
$arr1 = array("北京","上海","廣州");//索引數(shù)組
$smarty->assign("arr1",$arr1);//分配索引數(shù)組

$arr2 = array("city1"=>"北京","city2"=>"上海","city3"=>"廣州");//關(guān)聯(lián)數(shù)組
$smarty->assign("arr2",$arr2);//分配關(guān)聯(lián)數(shù)組

$arr3 = array(array("北京","上海","廣州"),array("關(guān)羽","張飛","美女"));
$smarty->assign("arr3",$arr3);

$arr4 = array("aa"=>array("北京","上海","廣州"),"bb"=>array("關(guān)羽","張飛","美女"));
$smarty->assign("arr4",$arr4);

//對象類型
class Master{
 public $name;
 public $address;
}
$master = new Master();
$master->name="百度";
$master->address = "中關(guān)村";
class Dog{
 public $name;
 public $age;
 public $color;
 public $arr;
 public $master;
 function __construct($name,$age,$color,$arr){
  $this->name = $name;
  $this->age = $age;
  $this->color = $color;
  $this->arr = $arr;
 }
}
$dog = new Dog("小狗",4,"金黃色",$arr2);
$dog->master = $master;
$smarty->assign("dog",$dog);

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

模板文件:index.tpl


復(fù)制代碼 代碼如下:

<html>
<h3>smarty變量操作</h3>
<p >取字符串:{$aa}</p>
<p >取整數(shù):{$bb}</p>
<p >取浮點(diǎn)型:{$cc}</p>
<p >取布爾值:{$dd}</p>
<p >取數(shù)組(索引數(shù)組):{$arr1[0]}--{$arr1[1]}--{$arr1[2]}</p>
<p >取數(shù)組(關(guān)聯(lián)數(shù)組):{$arr2.city1}--{$arr2.city2}--{$arr2.city3}</p>
<p >取二組數(shù)組(索引,取單個):{$arr3[0][0]}</p>
<p >取二組數(shù)組(索引,遍歷全部):</p>
<p >取二維數(shù)組(關(guān)聯(lián)):{$arr4.aa[2]}</p>
<p >取二維數(shù)組(關(guān)聯(lián)、遍歷):</p>
<p >取對象(普通屬性):{$dog->name}</p>
<p >取對象(數(shù)組屬性):{$dog->arr.city1}</p>
<p >取對象(對象屬性):{$dog->master->name}</p>
</html>

到此,關(guān)于“smarty模板引擎如何從php中獲取數(shù)據(jù)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

網(wǎng)站名稱:smarty模板引擎如何從php中獲取數(shù)據(jù)-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://muchs.cn/article34/ddccpe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、手機(jī)網(wǎng)站建設(shè)、移動網(wǎng)站建設(shè)、搜索引擎優(yōu)化企業(yè)建站、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)