十年網站開發(fā)經驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網站問題一站解決
PHP 模板smarty練習
一.練習環(huán)境
目前成都創(chuàng)新互聯(lián)已為千余家的企業(yè)提供了網站建設、域名、網站空間、網站托管、服務器租用、企業(yè)網站設計、興業(yè)網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
smarty_inc為smarty lib庫
smarty_inc.php導入庫文件
config_dir="Smarty/Config_File.class.php";
$smarty->caching=false; //緩存
$smarty->template_dir = "./templates"; //模板文件
$smarty->compile_dir = "./templates_c"; // 設定編譯文件的存儲路徑
//$smarty->cache_dir = "./smarty_cache";
$smarty->left_delimiter = "{";
$smarty->right_delimiter = "}";
?>
二.smarty變量練習
$smarty->assign 設置傳輸變量
$smarty->display 加載模板
index.php
assign('str',$str);
$smarty->display("index.html");
?>
index.html
smarty test
{$str}
訪問localhost/smarty/index.php 直接轉到index.html
this is my home!
三.smarty數(shù)組練習
index.php
assign('name',$students);
$smarty->display("index.html");
index.html
smarty test
{section name=stu loop=$name}
{$name[stu]}
{sectionelse}
無內容
{/section}
四.smarty二維數(shù)組練習
index.php
'sunqiang'];
$students[] = ['stu_name'=>'liuyao'];
$students[] = ['stu_name'=>'yuxx'];
$smarty->assign('name',$students);
$smarty->display("index.html");
index.html
smarty test
{section name=stu loop=$name}
{$name[stu].stu_name}
{sectionelse}
無內容
{/section}
五.smarty標量操作符練習
index.php
assign('str',$str);
$smarty->assign('time',time());
$smarty->assign('score',123.456);
$smarty->display("index.html");
index.html
smarty test
原始字符:{$str}
首字母大寫:{$str|capitalize}
計算字符數(shù):{$str|count_characters}
連接內容:{$str|cat:' i love study php'}
段落數(shù):{$str|count_paragraphs}
計算句數(shù):{$str|count_sentences}
計算單詞數(shù):{$str|count_words}
日期顯示:{$time|date_format:'%Y-%m-%d %H:%M:%S'} | {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'} | {$smarty.now}
默認顯示值:{$str1|default:'no content'}
轉碼:{$str|escape:url} | {$str|escape:html}
縮進:{$str|indent:5:'.'} | {$str|indent:5:'?'}
大寫:{$str|upper}
小寫:{$str|lower}
替換:{$str|replace:'my':'your'} | {$str|replace:'my':'**'}
字符串格式化:{$score|string_format:'%.2f'} | {$score|string_format:'%d'}
去空格:{$str|strip:''} | {$str|strip:'_'}
去html標簽:{$str|strip_tags}
截?。簕$str|truncate:10:'...'}
行寬約束:{$str|wordwrap:10:'
'}
六.smarty內置函數(shù)練習
1.有鍵值數(shù)組
index.php
'sq','teacher2'=>'ly','teacher3'=>'yxx'];
$smarty->assign('teacher_name',$arr_str);
$smarty->display("index.html");
index.html
smarty test
{foreach from=$teacher_name item=name key=teach}
數(shù)組內容:{$teach} - {$name}
{/foreach}
2.無鍵值數(shù)組
index.php
assign('teacher_name',$arr_str);
$smarty->display("index.html");
index.html
smarty test
{foreach from=$teacher_name item=name key=teach}
數(shù)組內容:{$teach} - {$name}
{/foreach}
index.php
assign('teacher_name',$arr_str);
$smarty->display("index.html");
index.html
smarty test
{foreach from=$teacher_name item=name}
數(shù)組內容:{$name}
{/foreach}
3.if條件語句
index.html
smarty test
條件語句:
{if name==''}
沒有設置內容
{else}
有設置內容
{/if}
因為index.php沒有設置這個變量,所有顯示沒有設置內容
4.include的使用
在模板下創(chuàng)建一個拼接文件head.html
index.html
smarty test
{include file='head.html'}
條件語句:
{if name==''}
沒有設置內容
{else}
有設置內容
{/if}
注:
{include file=‘d:\www\index2.php’} 可以是別的目錄下的引入文件
{include file='head.html' title=‘menu’}引入head.html,將title的值傳給head.html的中的變量title
5.literal的使用
literal數(shù)據(jù)將被當作文本處理,此時模板將忽略其內部的所有字符信息,該特性用于顯示有可能包含大括號等字符信息的javascript腳本,因為在模板設置中php的邊界設定為大括號,防止沖突。
{literal}
{/literal}
6.Strip的使用
strip標記中數(shù)據(jù)的首尾空格和回車,這樣可以保證模板容易理解且不用擔心多余的空格導致問題,使用會做整行的處理,但內容不變,同時節(jié)省了流量,還可以保護代碼。
使用時在html代碼 頭和尾
{strip}
{/strip}