PHP 模板smarty練習(xí)
一.練習(xí)環(huán)境
目前成都創(chuàng)新互聯(lián)已為千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、興業(yè)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
smarty_inc為smarty lib庫(kù)
smarty_inc.php導(dǎo)入庫(kù)文件
<?php
include_once ("smarty_inc/Smarty.class.php");
$smarty = new Smarty(); //實(shí)例化
$smarty->config_dir="Smarty/Config_File.class.php";
$smarty->caching=false; //緩存
$smarty->template_dir = "./templates"; //模板文件
$smarty->compile_dir = "./templates_c"; // 設(shè)定編譯文件的存儲(chǔ)路徑
//$smarty->cache_dir = "./smarty_cache";
$smarty->left_delimiter = "{";
$smarty->right_delimiter = "}";
?>二.smarty變量練習(xí)
$smarty->assign 設(shè)置傳輸變量
$smarty->display 加載模板
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示
$str = 'this is my home!';
$smarty->assign('str',$str);
$smarty->display("index.html");
?>index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smarty test</title>
</head>
<body>
{$str}
</body>
</html>訪問(wèn)localhost/smarty/index.php 直接轉(zhuǎn)到index.html
this is my home!
三.smarty數(shù)組練習(xí)
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示
$students = ['sunqing','liuyao','yuxx','王舞'];
$smarty->assign('name',$students);
$smarty->display("index.html");index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smarty test</title>
</head>
<body>
{section name=stu loop=$name}
{$name[stu]}
{sectionelse}
無(wú)內(nèi)容
{/section}
</body>
</html>
四.smarty二維數(shù)組練習(xí)
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示
$students[] = ['stu_name'=>'sunqiang'];
$students[] = ['stu_name'=>'liuyao'];
$students[] = ['stu_name'=>'yuxx'];
$smarty->assign('name',$students);
$smarty->display("index.html");index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smarty test</title>
</head>
<body>
{section name=stu loop=$name}
{$name[stu].stu_name}
{sectionelse}
無(wú)內(nèi)容
{/section}
</body>
</html>
五.smarty標(biāo)量操作符練習(xí)
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示
$str = 'this is my home!';
$smarty->assign('str',$str);
$smarty->assign('time',time());
$smarty->assign('score',123.456);
$smarty->display("index.html");index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smarty test</title>
</head>
<body>
原始字符:{$str}<br>
<hr>
首字母大寫:{$str|capitalize}<br>
計(jì)算字符數(shù):{$str|count_characters}<br>
連接內(nèi)容:{$str|cat:' i love study php'}<br>
段落數(shù):{$str|count_paragraphs}<br> <!--回車計(jì)算段落數(shù)-->
計(jì)算句數(shù):{$str|count_sentences}<br>
計(jì)算單詞數(shù):{$str|count_words}<br>
日期顯示:{$time|date_format:'%Y-%m-%d %H:%M:%S'} | {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'} | {$smarty.now}<br>
默認(rèn)顯示值:{$str1|default:'no content'}<br>
轉(zhuǎn)碼:{$str|escape:url} | {$str|escape:html}<br>
縮進(jìn):{$str|indent:5:'.'} | {$str|indent:5:'?'}<br>
大寫:{$str|upper}<br>
小寫:{$str|lower}<br>
替換:{$str|replace:'my':'your'} | {$str|replace:'my':'**'}<br><!--屏蔽關(guān)鍵詞-->
字符串格式化:{$score|string_format:'%.2f'} | {$score|string_format:'%d'}<br><!--保留小數(shù)點(diǎn)2位,保留整數(shù)-->
去空格:{$str|strip:''} | {$str|strip:'_'}<br>
去html標(biāo)簽:{$str|strip_tags}<br>
截取:{$str|truncate:10:'...'}<br>
行寬約束:{$str|wordwrap:10:'<br>'}
</body>
</html>
六.smarty內(nèi)置函數(shù)練習(xí)
1.有鍵值數(shù)組
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示
$arr_str = ['teacher1'=>'sq','teacher2'=>'ly','teacher3'=>'yxx'];
$smarty->assign('teacher_name',$arr_str);
$smarty->display("index.html");index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smarty test</title>
</head>
<body>
{foreach from=$teacher_name item=name key=teach}
數(shù)組內(nèi)容:{$teach} - {$name}<br>
{/foreach}
</body>
</html>
2.無(wú)鍵值數(shù)組
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示
$arr_str = ['sq','ly','yxx'];
$smarty->assign('teacher_name',$arr_str);
$smarty->display("index.html");index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smarty test</title>
</head>
<body>
{foreach from=$teacher_name item=name key=teach}
數(shù)組內(nèi)容:{$teach} - {$name}<br>
{/foreach}
</body>
</html>
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報(bào)錯(cuò),255列出所有提示,0關(guān)閉所有提示
$arr_str = ['sq','ly','yxx'];
$smarty->assign('teacher_name',$arr_str);
$smarty->display("index.html");index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smarty test</title>
</head>
<body>
{foreach from=$teacher_name item=name}
數(shù)組內(nèi)容:{$name}<br>
{/foreach}
</body>
</html>
3.if條件語(yǔ)句
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smarty test</title>
</head>
<body>
條件語(yǔ)句:
{if name==''}
沒(méi)有設(shè)置內(nèi)容
{else}
有設(shè)置內(nèi)容
{/if}
</body>
</html>因?yàn)閕ndex.php沒(méi)有設(shè)置這個(gè)變量,所有顯示沒(méi)有設(shè)置內(nèi)容
4.include的使用
在模板下創(chuàng)建一個(gè)拼接文件head.html
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smarty test</title>
</head>
<body>
{include file='head.html'}
條件語(yǔ)句:
{if name==''}
沒(méi)有設(shè)置內(nèi)容
{else}
有設(shè)置內(nèi)容
{/if}
</body>
</html>
注:
{include file=‘d:\www\index2.php’} 可以是別的目錄下的引入文件
{include file='head.html' title=‘menu’}引入head.html,將title的值傳給head.html的中的變量title
5.literal的使用
literal數(shù)據(jù)將被當(dāng)作文本處理,此時(shí)模板將忽略其內(nèi)部的所有字符信息,該特性用于顯示有可能包含大括號(hào)等字符信息的javascript腳本,因?yàn)樵谀0逶O(shè)置中php的邊界設(shè)定為大括號(hào),防止沖突。
{literal}
<script language=javascript>
......
</script>
{/literal}6.Strip的使用
strip標(biāo)記中數(shù)據(jù)的首尾空格和回車,這樣可以保證模板容易理解且不用擔(dān)心多余的空格導(dǎo)致問(wèn)題,使用會(huì)做整行的處理,但內(nèi)容不變,同時(shí)節(jié)省了流量,還可以保護(hù)代碼。
使用時(shí)在html代碼 頭和尾
{strip}
<html>
</html>
{/strip}
新聞名稱:PHP模板smarty練習(xí)
URL網(wǎng)址:http://chinadenli.net/article24/jhjdce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、商城網(wǎng)站、域名注冊(cè)、小程序開(kāi)發(fā)、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)