国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

smarty模板引擎從php中獲取數(shù)據(jù)的方法

2024-05-04 23:30:25
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了smarty模板引擎從php中獲取數(shù)據(jù)的方法,涉及smarty變量與php代碼的混編技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
 

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

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

操作/顯示文件:index.php

復(fù)制代碼代碼如下:
<?php
//創(chuàng)建smarty對(duì)象
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ù)庫(kù)取出,這里直接給數(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);

//對(duì)象類(lèi)型
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>
<h2>smarty變量操作</h2>
<p style="color:green;">取字符串:{$aa}</p>
<p style="color:red;">取整數(shù):{$bb}</p>
<p style="color:blue;">取浮點(diǎn)型:{$cc}</p>
<p style="color:orange;">取布爾值:{$dd}</p>
<p style="color:indigo;">取數(shù)組(索引數(shù)組):{$arr1[0]}--{$arr1[1]}--{$arr1[2]}</p>
<p style="color:green;">取數(shù)組(關(guān)聯(lián)數(shù)組):{$arr2.city1}--{$arr2.city2}--{$arr2.city3}</p>
<p style="color:red;">取二組數(shù)組(索引,取單個(gè)):{$arr3[0][0]}</p>
<p style="color:red;">取二組數(shù)組(索引,遍歷全部):</p>
<p style="color:blue;">取二維數(shù)組(關(guān)聯(lián)):{$arr4.aa[2]}</p>
<p style="color:blue;">取二維數(shù)組(關(guān)聯(lián)、遍歷):</p>
<p style="color:orange;">取對(duì)象(普通屬性):{$dog->name}</p>
<p style="color:orange;">取對(duì)象(數(shù)組屬性):{$dog->arr.city1}</p>
<p style="color:orange;">取對(duì)象(對(duì)象屬性):{$dog->master->name}</p>
</html>

 

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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大余县| 哈尔滨市| 肃南| 定南县| 新丰县| 楚雄市| 仪征市| 东至县| 陈巴尔虎旗| 四子王旗| 平远县| 临城县| 苏尼特左旗| 镇巴县| 博客| 华亭县| 大安市| 怀柔区| 辽阳县| 乌拉特前旗| 营口市| 姚安县| 那坡县| 微山县| 吐鲁番市| 昆明市| 万源市| 鄂托克前旗| 西峡县| 焉耆| 永嘉县| 伊宁县| 舟山市| 安龙县| 永胜县| 台南县| 惠州市| 南丰县| 凤凰县| 保定市| 内黄县|