- web服務(wù)
web服務(wù)用的是SOAP(簡(jiǎn)單對(duì)象訪問(wèn)協(xié)議):是web服務(wù)的通信協(xié)議,用來(lái)定義返回消息的XML格式的規(guī)范
wsdl:web服務(wù)描述語(yǔ)言,包括一系列web服務(wù)的定義。
注意:PHP默認(rèn)是不支持soap協(xié)議的,要開(kāi)啟soap協(xié)議
extension=php_soap.dll
<?php
$client = new SoapClient("http://webserver.com");
$city=$client->getDomesticCity();
echo "<pre>"
var_dum($city);
?>
- Smarty:用來(lái)將顯示和內(nèi)容相分離的框架
PHP代碼 + HTML代碼-->經(jīng)過(guò)smarty將HTML中的定界符轉(zhuǎn)換成PHP的定界符-->混編文件
//自定義smarty
<?php
$title = "靜夜思";
$content = "床前明月光,疑似地上霜,舉頭望明月,低頭思故鄉(xiāng).";
$str = file_get_contents('./demo.html');
$str=str_replace('{','<?php echo $',$str);
$str=str_replace('}',';?>',$str);
file_put_contents('./demo.html.php',$str);
require("./demo.html.php");
?>
優(yōu)化smarty
上面例題中替換定界符的代碼在每個(gè)PHP頁(yè)面中都要出現(xiàn),這樣的代碼不健壯,
解決:
將公共的代碼封裝到Smarty的compile()方法中
public function compile(){
$str = file_get_contents('./demo.html');
$str=str_replace('{','<?php echo $this->tpl_var["',$str);
$str=str_replace('}','"];?>',$str);
file_put_contents('./demo.html.php',$str);
require("./demo.html.php");
}
封裝后我們發(fā)現(xiàn)在類(lèi)的內(nèi)部不能輸出類(lèi)外面的變量。
解決:
定義一個(gè)私有屬性用來(lái)保存變量,再定義一個(gè)共有的方法對(duì)私有的成員變量進(jìn)行賦值。
<?php
class Smarty{
private $tpl_var=array();
public function assign($k,$v){
$this->tpl_var[$k] = $v;
}
}
?>
優(yōu)化Smarty(二)
上面的缺點(diǎn):每次執(zhí)行頁(yè)面,compile中替換字符串的代碼都要執(zhí)行,這樣影響執(zhí)行效率,
解決:
如果混編文件存在,并且混編文件的修改時(shí)間大于模板修改時(shí)間,則直接引入。否則重新生成。
public function compile(){
$compile_path=$tpl.'.php';
if(file_exists($compile_path) && filetime($compile_path) > filetime($tpl)){
require $compile_path;
}else {
echo 'create<br/>';
$str = file_get_contents($tpl);
$str=str_replace('{','<?php echo $this->tpl_var["',$str);
$str=str_replace('}','"];?>',$str);
file_put_contents($compile_path,$str);
require($compile_path);
}
}
優(yōu)化Smarty(三)
上面的缺點(diǎn):所有的模板和混編文件都放在一起,不便于管理
解決:
新建一個(gè)模板文件夾(view,template),和編譯文件夾(view_c,template_c),用來(lái)存放對(duì)應(yīng)的文件
使用smarty
Smarty.class.php
templates:默認(rèn)存放模板文件夾
templates_c:默認(rèn)存放混編文件的文件夾
cache:存放緩存
configs:存放配置文件
注釋
{*這是注釋*}
- smarty 的基礎(chǔ)
普通變量
1、第一種聲明方法:
require './Smarty/Smarty.class.php';
$smarty = new Smarty();
$smarty->assign("name","李白");
$smarty->display("demo.html");
2、第二種聲明方法:
在模板文件中聲明
{assign var="變量名" value="值"}
3.取值(不管聲明的方法如何,取值方法一樣)
{$name}
{$sex}
保留變量
在Smarty中有一個(gè)特殊的變量“smarty”,這個(gè)變量是保留變量,用來(lái)訪問(wèn)用戶請(qǐng)求的信息,系統(tǒng)環(huán)境變量,常亮,類(lèi)似于PHP中的超全局變量。
1. 獲得get提交的值:例如:{$smarty.get.name} $_GET
2. 獲得post提交的值 {$smarty.post.變量名} $_POST
3. 萬(wàn)能的獲得值的方法 {$smarty.request.變量名} $_REQUEST
4. 獲取會(huì)話:
在PHP中定義一個(gè)會(huì)話
$_SESSION["country"] = "China";
在模板中獲取會(huì)話
{$smarty.session.country}
5、獲取cookie
setCookie("school","peking university");
{$smarty.cookies.school}
6、顯示常量
define("city","shenzhen");
{$smarty.const.city}
7、取出服務(wù)器信息
{$smarty.server.REMOTE_ADDR}
8、取出當(dāng)前時(shí)間
{$smarty.now}
9、其他的一些變量
{$smarty.version}
{$smarty.ldelim}
{$smarty.rdelim}
配置變量
1、在站點(diǎn)下新建configs文件夾,在文件夾中新建smarty.conf文件,代碼如下:
host=127.0.0.1
username=root
pwd=aaa
2、在模板文件中導(dǎo)入配置文件,并取值
{config_load file="smarty.conf"}
{#host#}<br>
{#username#}<br>
{#pwd#}
注意:
smarty將css的{解析了。
解決方法:
1.更換定界符。
2.在{后加空格
3.使用{literal}{/literal}
{literal}
<style>
div{...}
</style>
{/literal}
3、配置文件中的章節(jié)
[style1] //定義章節(jié)
width="300px"
height="400px"
border="1 solid #F00"
backgroundcolor="#555";
調(diào)用章節(jié)中配置的變量
{config_load file="smarty.conf" section="style1"}
配置變量的注意:
(1)配置文件中的注釋是“#”
(2)中括號(hào)表示節(jié)
(3)節(jié)定義的相當(dāng)于局部變量
數(shù)組
smarty對(duì)數(shù)組的訪問(wèn)
1. 數(shù)組[下標(biāo)]
2. 數(shù)組.下標(biāo)
例如:
$smarty->assign("stu",array("tom","berry","ketty"));
$smarty->assign("emp",array("name"=>"libai","gender"=>"male"));
$smarty->assign('teacher',array(array("name"=>"libai","gender"=>"male"),array("name"=>"dufu","gender"=>"female")));
//訪問(wèn)
{$stu[0]} {$stu.0}
{$emp.name} {$emp["name"]}
{$teacher[0]["name"]}
{$teacher.0.["name"]}
{$teacher.0["name"]}
{$teacher[0].name}
foreach
如果不存在遍歷的數(shù)組則執(zhí)行{foreachelse}部分
{foreach $stu as $k=>$v}
{$k}:{$v}
{foreachelse}
{/foreach}
foreach內(nèi)部關(guān)鍵字
| 語(yǔ)法 | 描述 |
|---|---|
| 值變量@iteration | 從1開(kāi)始的序號(hào) |
| 值變量@index | 從0開(kāi)始的索引 |
| 值變量@first | 判斷是否是第一個(gè)元素 |
| 值變量@last | 判斷是否是最后一個(gè)元素 |
| 值變量@total | 數(shù)組的長(zhǎng)度 |
| 值變量@show | 數(shù)組是否為空 |
<table>
<tr>
<th>是否是第一個(gè)元素</th>
<th>編號(hào)</th>
<th>索引</th>
<th>鍵</th>
<th>值</th>
<th>是否是最后元素</th>
</tr>
{foreach $stu as $k->$v}
{if $v@first}
<tr class="first">
{elseif $v@last}
<tr class="last">
{elseif $v@iteration%2==0}
<tr class="even">
{else}
<tr>
</if>
<td>{$v@first}</td>
<td>{$v@iteration}</td>
<td>{$v@index}</td>
<td>{$k}</td>
<td>{$v}</td>
<td>{$v@last}</td>
{foreachelse}
no such array;
{/foreach}
</table>
判斷
{if 條件}
{elseif 條件}
{else}
{/if}
section循環(huán)
只支持索引數(shù)組,不支持關(guān)聯(lián)數(shù)組。
{section name='自定義變量名' loop='被遍歷的數(shù)組'}
{數(shù)組['自定義變量名']}
{/section}
section內(nèi)置的關(guān)鍵字
{$smarty.section.自定義變量名.iteration}
{$smarty.section.自定義變量名.index}
{$smarty.section.自定義變量名.first}
{$smarty.section.自定義變量名.last}
{section name=s loop=$stu}
{$stu[s]}-{$smarty.section.s.iteration}-{$smarty.section.s.index}
{/section}
for循環(huán)
{for $i=1 to 10}
{$i}: Hello world<br>
{/for}
{for $i=1 to 10 step 2}
{$i} : Hello world<br>
{/for}
while循環(huán)
{assign var='i' value=1}
{while $i<10}
{$i++} : hello world<br>
{/while}
html_checkbox
1.
寫(xiě)法一
$smarty->assign('output',array('eating','play','sleep','swiming'));
<p>
hobby:{html_checkboxes output=$output values=$value selected=$selected name='hobby' separator="<br>"}
</p>
2.寫(xiě)法二
$smarty->assign('options',array('a'=>'eating','b'=>'play','c'=>'sleep','d'=>'swiming'));
$smarty->assign('selected',array('a','c'));
<p>
{html_checkboxes options=$options name='hobby' selected=$selected};
</p>
html_options:
<p>
{html_options options=$options name='hobby' selected=$selected multiple='multiple'};
</p>
html_radios:
{html_radios options=$options name='hobby' selected=$selected_radio};
- 把已有的HTML和Smarty結(jié)合
1.在站點(diǎn)下新建public文件夾,在public文件夾中新建style,js,images文件夾,將素材copy到相對(duì)應(yīng)的目錄;
2.更改路徑
<link href="/public/style/style.css" rel="stylesheet" type="text/css">
<script src="/public/js/jquery.js">
</script>
<script src="/public/js/my.js">
</script>
<style>
p{background:url(../imges.third_03.jpg) over;}
</style>
注意:HTML頁(yè)面中素材的路徑從根開(kāi)始匹配,CSS中按當(dāng)前路徑開(kāi)始匹配。
3.PHP代碼
require './Smarty/Smarty.class.php';
$smarty = new $smarty();
$ticket = array(
array('世界',100,23,'電子/紙質(zhì)','>90人');
array('世界',120,21,'電子/紙質(zhì)','>90人');
array('世界',150,24,'電子/紙質(zhì)','>90人');
array('世界',1060,2,'電子/紙質(zhì)','>90人');
);
$smarty->assign('ticket',$ticket);
$smarty->display('index.html');
4.html模板代碼
{foreach $ticket as $v}
<tr>
<td height="25"> align="center"><input name="checkbox" value=""></td>
<td align="center">{$v.0}</td>
<td align="center">{$v.1}</td>
<td align="center">{$v.2}</td>
<td align="center">{$v.3}</td>
<td align="center">{$v.4}</td>
</tr>
</foreach}
- Smarty布局
一.
1.在站點(diǎn)下新建文件夾layout,在文件夾中新建一個(gè)template.html模板
<body>
<h2>Head</h2>
{block name="main"}{/block} //通過(guò)名字來(lái)替換
<h3>Footer</h3>
</body>
2.在模板中的代碼
{extends file="layout/template.html"} //繼承布局文件
{block name="main"}<strong>舉頭望明月,低頭思故鄉(xiāng).</strong>{/block} //替換
一個(gè)模板中可以由多個(gè){block}
<body>
<h2>Head</h2>
{block name="main"}{/block} //通過(guò)名字來(lái)替換
<hr/>
{block name="main2"}{/block}
<h3>Footer</h3>
</body>
{extends file="layout/template.html"} //繼承布局文件
{block name="main"}<strong>舉頭望明月,低頭思故鄉(xiāng).</strong>{/block} //替換
{block name="main2"}
過(guò)年我要回家!!!!
{/block}
二.
頭,中間(左右),腳
可以把中間的部分全部替換,也可以把右邊的部分替換,這樣就可以實(shí)現(xiàn)一個(gè)布局文件用于多個(gè)模板。
1、新建模板:template.1.html
<body>
<h2>Head</h2>
<block name="main">
<div id="main">
<div id="left">
Link1<br>
Link2<br>
Link3<br>
</div>
{block name="right"}
<div id="right"></div>
{/block}
</div>
</block>
<h3>Footer</h3>
</body>
2.替換main
{extends file="layout/template.1.html"}
{block name="main"}
床前明月光,疑是地上霜.舉頭望明月,低頭思故鄉(xiāng).
{/block}
替換right
{extends file="layout/template.1.html"}
{block name="right"}
床前明月光,疑是地上霜.舉頭望明月,低頭思故鄉(xiāng).
{/block}
包含文件
{include file='文件的路徑'}
如果公共部分多:用布局文件
如果公共部分少,用包含文件。
變量修飾器
在用“truncate”截取字符串的時(shí)候,默認(rèn)情況下用utf8編碼,一個(gè)中文在utf8下占用3個(gè)字節(jié),所以容易出現(xiàn)亂碼。
解決方法:
開(kāi)啟 extension=php_mbstring.dll;
緩存
require './Smarty/Smarty.class.php';
$smarty=new Smarty();
$smarty->caching=1; //開(kāi)啟
$smarty->caching=1 | true;開(kāi)啟smarty緩存,調(diào)用display()的時(shí)候,如有有緩存就從緩存中讀取。
緩存文件的更新
1、刪除對(duì)應(yīng)的緩存
2、強(qiáng)制更新 $smarty->force_cache
3、更新模板,布局文件、包含文件。
4、設(shè)置緩存的生命周期。
$smarty->cache_lifetime=5; //5秒
運(yùn)算符
1、算術(shù)運(yùn)算符
一元運(yùn)算符
++
--
- 一元減(負(fù)號(hào))
二元運(yùn)算符
+
-
*
/
%
三元運(yùn)算符
條件?值1:值2
2、關(guān)系運(yùn)算符
>
>=
<
<=
==
!=
3邏輯運(yùn)算符
&&
||
!