您好,登錄后才能下訂單哦!
PHP 模板smarty練習
一.練習環境
smarty_inc為smarty lib庫
smarty_inc.php導入庫文件
<?php
include_once ("smarty_inc/Smarty.class.php");
$smarty = new Smarty(); //實例化
$smarty->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
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出所有提示,0關閉所有提示
$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>
訪問localhost/smarty/index.php 直接轉到index.html
this is my home!
三.smarty數組練習
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出所有提示,0關閉所有提示
$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}
無內容
{/section}
</body>
</html>
四.smarty二維數組練習
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出所有提示,0關閉所有提示
$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}
無內容
{/section}
</body>
</html>
五.smarty標量操作符練習
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出所有提示,0關閉所有提示
$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>
計算字符數:{$str|count_characters}<br>
連接內容:{$str|cat:' i love study php'}<br>
段落數:{$str|count_paragraphs}<br> <!--回車計算段落數-->
計算句數:{$str|count_sentences}<br>
計算單詞數:{$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>
默認顯示值:{$str1|default:'no content'}<br>
轉碼:{$str|escape:url} | {$str|escape:html}<br>
縮進:{$str|indent:5:'.'} | {$str|indent:5:'?'}<br>
大寫:{$str|upper}<br>
小寫:{$str|lower}<br>
替換:{$str|replace:'my':'your'} | {$str|replace:'my':'**'}<br><!--屏蔽關鍵詞-->
字符串格式化:{$score|string_format:'%.2f'} | {$score|string_format:'%d'}<br><!--保留小數點2位,保留整數-->
去空格:{$str|strip:''} | {$str|strip:'_'}<br>
去html標簽:{$str|strip_tags}<br>
截取:{$str|truncate:10:'...'}<br>
行寬約束:{$str|wordwrap:10:'<br>'}
</body>
</html>
六.smarty內置函數練習
1.有鍵值數組
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出所有提示,0關閉所有提示
$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}
數組內容:{$teach} - {$name}<br>
{/foreach}
</body>
</html>
2.無鍵值數組
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出所有提示,0關閉所有提示
$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}
數組內容:{$teach} - {$name}<br>
{/foreach}
</body>
</html>
index.php
<?php
include ("smarty_inc.php");
error_reporting(7);//不顯示警告報錯,255列出所有提示,0關閉所有提示
$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}
數組內容:{$name}<br>
{/foreach}
</body>
</html>
3.if條件語句
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smarty test</title>
</head>
<body>
條件語句:
{if name==''}
沒有設置內容
{else}
有設置內容
{/if}
</body>
</html>
因為index.php沒有設置這個變量,所有顯示沒有設置內容
4.include的使用
在模板下創建一個拼接文件head.html
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>smarty test</title>
</head>
<body>
{include file='head.html'}
條件語句:
{if name==''}
沒有設置內容
{else}
有設置內容
{/if}
</body>
</html>
注:
{include file=‘d:\www\index2.php’} 可以是別的目錄下的引入文件
{include file='head.html' title=‘menu’}引入head.html,將title的值傳給head.html的中的變量title
5.literal的使用
literal數據將被當作文本處理,此時模板將忽略其內部的所有字符信息,該特性用于顯示有可能包含大括號等字符信息的javascript腳本,因為在模板設置中php的邊界設定為大括號,防止沖突。
{literal}
<script language=javascript>
......
</script>
{/literal}
6.Strip的使用
strip標記中數據的首尾空格和回車,這樣可以保證模板容易理解且不用擔心多余的空格導致問題,使用會做整行的處理,但內容不變,同時節省了流量,還可以保護代碼。
使用時在html代碼 頭和尾
{strip}
<html>
</html>
{/strip}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。