1/1
課程視頻:smarty 模板
00:00 / 00:00
倍速
0.5
0.75
1.0
1.25
1.5
2.0
2.5
3.0
Player version:
4.6.2
Video url:
https://7nveo.w3cschool.cn/ax_w3cphpframe_16048zm.mp4?e=1745875984&token=6ayIfCP4z1e7Hg3p3yw4W4Q6QgqIBr1jY7x-8K4P:EtzZOe8gwAt_PESj3soK6frLeJc=
Video volume:
0.70
Video time:
0.00
Video duration:
NaN
Video resolution:
0.00 x 0.00
[x]
課程視頻:smarty 模板
×
smarty模版引擎技術(shù)需要掌握的知識點
- smarty 的特點:做到 HTML 與 PHP 代碼分離。
- smarty 模版的搭建:
- 具體代碼流程 (index.php)。
//1.導(dǎo)入Smarty類;
require "./libs/Smarty.class.php";
//2.創(chuàng)建對象
$smarty = new Smarty();
//3.初始化信息
$smarty->left_delimiter = "{"; //重新定義Smarty模板的左定界符
$smarty->right_delimiter = "}"; //重新定義Smarty模板的右定界符
$smarty->template_dir="view";//設(shè)置模版目錄
$smarty->compile_dir="./view_c";//設(shè)置編譯目錄
//靜態(tài)緩存
$smarty->caching = true; //是否開啟靜態(tài)緩存 true(開啟)
$smarty->cache_lifetime = 100; //設(shè)置緩存時間 (5表示緩存5秒鐘)只在瀏覽器中,不是動態(tài);
//4.放置變量:
$name = "黃小明";
$_POST['classid'] = "w3c001";
const N = 100;
$arr = array('name'=>'小紅','age'=>18);
$smarty->assign("name",$name);//向Smarty模板中放置變量name值為張三
$smarty->assign("date",date("Y-m-d H:i:s"));//為模板放一個時間
$smarty->assign('list',$arr);
//5.加載模板:
$smarty->display("view/index.html");
(視圖代碼 :index.html)
<!DOCTYPE html>
{config_load file="myself.conf" section="en"}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{#title#}</title>
</head>
<body bgcolor="{$smarty.config.bgcolor}">
<center>
{include file="view/menu.html"}
<h1>{$name}</h1>
<h1>{$smarty.post.classid}</h1>
<h1>{$smarty.const.N}</h1>
<hr>
{foreach $list as $k=>$v}
<h2>{$k}=>{$v}</h2>
{if $v==181}
<h3>Learn code to W3Cschool!</h3>
{/if}
{/foreach}
<hr>
<form>
{#username#}<input type="text" name="name"><br><br>
{#pwd#}<input type="password" name="pwd"><br><br>
</form>
</center>
</body>
</html>
- 總結(jié)。
- 掌握搭建步驟。
- 掌握兩個常用方法。
assign('變量名' ,'變量值|數(shù)組');
display('跳轉(zhuǎn)的路徑');
- 掌握
if
、foreach
流程控制。
- 掌握
include
引用文件。
- 了解
$smarty
保留字和config.conf
全局和局部配置。
configs
路徑:
{config_load file=”配置文件名” section=”zh”}
注:
section
表示局部。