概念
官方地址
www.smarty.net
Smarty常用屬性
public $left_delimiter = "{";
左界定public $right_delimiter = "}";
右界定protected $template_dir = array('./templates/');
默認(rèn)模板目錄protected $compile_dir = './templates_c/';
默認(rèn)混編目錄protected $config_dir = array('./configs/');
默認(rèn)配置目錄protected $cache_dir = './cache/';
默認(rèn)緩存目錄Smarty常用方法
public function setTemplateDir(){}
設(shè)置模板文件夾public function setConfigDir(){}
設(shè)置配置文件夾public function setCompileDir(){}
設(shè)置混編文件夾public function setCacheDir(){}
設(shè)置緩存文件夾小試牛刀
<?phprequire './Smarty/Smarty.class.php';$smarty=new Smarty();$smarty->assign('title','鋤禾日當(dāng)午');$smarty->left_delimiter='{{';//更改左界定$smarty->right_delimiter='}}';//更改右界定$smarty->setTemplateDir('./templates/');//設(shè)置模板目錄$smarty->setCompileDir('./templates_c/');//設(shè)置混編目錄$smarty->display('index.html');?>
# index.html<body>{{$title}}</body>
# index.html<body> {$title}</body>
# index.php<?php$title= 'Smarty';$str=file_get_contents('./index.html');$str=str_replace('{','<?php echo ',$str); //替換左大括號(hào)$str=str_replace('}',';?>',$str); //替換右大括號(hào)file_put_contents('./index.html.php', $str);//寫(xiě)入混編文件require './index.html.php';//包含混編文件?>
# index.html.php<body> <?php echo $title;?></body>
<?php $title= 'Smarty';?><body> <?php echo $title;?></body>
# Smarty.class.php<?phpclass Smarty{private $tpl_var=array();//賦值public function assign($k,$v){$this->tpl_var[$k]=$v;}/**作用:編譯模板*@param $tpl string 模板的路徑*/public function compile($tpl){$com_file=$tpl.'.php'; //混編文件地址$str=file_get_contents($tpl);$str=str_replace('{$','<?php echo $this->tpl_var[\'',$str);//替換左大括號(hào)$str=str_replace('}','\'];?>',$str); //替換右大括號(hào)file_put_contents($com_file, $str); //寫(xiě)入混編文件require $com_file; //包含混編文件}}?>
# index.html<body> {$title}</body>
# index.php<?phprequire './Smarty.class.php';$smarty=new Smarty();$smarty->assign('title','我的祖國(guó)');$smarty->compile('./index.html');?>
# Smarty.class.php<?phpclass Smarty{private $tpl_var=array();//賦值public function assign($k,$v){$this->tpl_var[$k]=$v;}/**作用:編譯模板*@param $tpl string 模板的路徑*/public function compile($tpl){ $com_file=$tpl.'.php'; //混編文件地址 //文件存在,并且模板文件修改時(shí)間<混編文件修改時(shí)間if(file_exists($com_file) && filemtime($tpl)<filemtime($com_file)) require $com_file; else{ $str= file_get_contents($tpl); $str= str_replace('{$','<?php echo $this->tpl_var[\'',$str);//替換左大括號(hào) $str= str_replace('}','\'];?>',$str); //替換右大括號(hào) file_put_contents($com_file, $str); //寫(xiě)入混編文件 require $com_file; //包含混編文件 } }}?>
# Smarty.class.php<?phpclass Smarty{public $template_dir='./templates/';//默認(rèn)模板目錄public $templatec_dir='./templates_c/';//默認(rèn)混編目錄private $tpl_var=array();//賦值public function assign($k,$v){$this->tpl_var[$k]=$v;}/**作用:編譯模板*@param $tpl string 模板的名字*/public function compile($tpl){$tpl_file=$this->template_dir.$tpl;//拼接模板地址$com_file=$this->templatec_dir.$tpl.'.php';//混編文件地址//文件存在,并且模板文件修改時(shí)間<混編文件修改時(shí)間if(file_exists($com_file) && filemtime($tpl_file)<filemtime($com_file))require $com_file;else{$str=file_get_contents($tpl_file);$str=str_replace('{$','<?php echo $this->tpl_var[\'',$str);//替換左大括號(hào)$str=str_replace('}','\'];?>',$str);//替換右大括號(hào)file_put_contents($com_file, $str);//寫(xiě)入混編文件require $com_file;//包含混編文件}}}?>
# index.php<?phprequire './Smarty/Smarty.class.php';$smarty=new Smarty();$smarty->template_dir='./view/';//更改模板目錄$smarty->templatec_dir='./viewc/';//更改混編目錄$smarty->assign('title','Sunny');$smarty->compile('index.html');?>
# Smarty.class.php<?phpclass Smarty{public $template_dir='./templates/';//默認(rèn)模板目錄public $templatec_dir='./templates_c/';//默認(rèn)混編目錄private $tpl_var=array();//賦值public function assign($k,$v){$this->tpl_var[$k]=$v;}public function display($tpl){require $this->compile($tpl);}/**作用:編譯模板*@param $tpl string 模板的名字*/private function compile($tpl){$tpl_file=$this->template_dir.$tpl;//拼接模板地址$com_file=$this->templatec_dir.$tpl.'.php';//混編文件地址//文件存在,并且模板文件修改時(shí)間<混編文件修改時(shí)間if(file_exists($com_file) && filemtime($tpl_file)<filemtime($com_file))return $com_file;else{$str=file_get_contents($tpl_file);$str=str_replace('{$','<?php echo $this->tpl_var[\'',$str);//替換左大括號(hào)$str=str_replace('}','\'];?>',$str);//替換右大括號(hào)file_put_contents($com_file, $str);//寫(xiě)入混編文件return $com_file;//包含混編文件}}}?>
# index.php<?phprequire './Smarty/Smarty.class.php';$smarty=new Smarty();$smarty->template_dir='./view/';//更改模板目錄$smarty->templatec_dir='./viewc/';//更改混編目錄$smarty->assign('title','Sunny');$smarty->display('index.html');?>
語(yǔ)法
{* *}
說(shuō)明
{* *}
,則它的注釋是{** **}
$smarty->assign('key','value');
{assign var='變量名' value='值'}
# index.php<?phprequire './Smarty/Smarty.class.php';$smarty=new Smarty();$smarty->assign('name','Sunny');$smarty->left_delimiter='{{';//更改左界定$smarty->right_delimiter='}}';//更改右界定$smarty->setTemplateDir('./templates/');//設(shè)置模板目錄$smarty->setCompileDir('./templates_c/');//設(shè)置混編目錄$smarty->display('index.html');?>
# index.html<body>姓名:{{$name}}<br>{{assign var='age' value='28'}}年齡:{{$age}}<br>{{$grade='高三8班'}}班級(jí):{{$grade}}<br></body>
概念
內(nèi)置變量
{$smarty.get.name}
獲取get提交的name的值{$smarty.post.name}
獲取post提交的name的值{$smarty.request.name}
獲取get和post提交的name的值{$smarty.cookies.name}
獲取cookie中的name的值{$smarty.session.name}
獲取session中的name的值{$smarty.const.name}
獲取常量定義的name值{$smarty.server.DOCUMENT_ROOT}
獲取服務(wù)器的虛擬根目錄地址{$smarty.config.name}
獲取配置文件中的值{$smarty.now}
時(shí)間戳{$smarty.ldelim}
獲取左界定{$smarty.rdelim}
獲取右界定# index.php<?phprequire './Smarty/Smarty.class.php';$smarty=new Smarty();define('name','define value');setcookie('name','cookie value');$_SESSION['name']='session value';$_POST['name']='post value';$_GET['name']='get value';$_REQUEST['name']='request value';$smarty->left_delimiter='{';//更改左界定$smarty->right_delimiter='}';//更改右界定$smarty->setTemplateDir('./templates/');//設(shè)置模板目錄$smarty->setCompileDir('./templates_c/');//設(shè)置混編目錄$smarty->display('index.html');?>
# index.html<body>get提交:{$smarty.get.name}<br>post提交:{$smarty.post.name}<br>request提交:{$smarty.request.name}<br>常量:{$smarty.const.name}<br>cookie的值:{$smarty.cookies.name}<br>session的值:{$smarty.session.name}<br>時(shí)間戳:{$smarty.now}<br>版本號(hào):{$smarty.version}<br>根目錄:{$smarty.server.DOCUMENT_ROOT}<br>左界定:{$smarty.ldelim}<br>右界定:{$smarty.rdelim}<br></body>
概念
configs
流程
configs
configs
目錄下創(chuàng)建smarty.conf文件# smarty.confcolor= '#21f4b1';size='15px';
# index.php<?phprequire './Smarty/Smarty.class.php';$smarty=new Smarty();$smarty->display('index.html');?>
# index.html{config_load file='smarty.conf'} <!--引入配置文件--><style>body{color:{$smarty.config.color};font-size: {#size#}}</style><body><span>測(cè)試文本</span></body>
[ ]
表示配置文件的段落[ ]
表示節(jié)#
section
引入配置文件中的段落# smarty.confcolor= #1e6bec;size=32px;[spring]# 配置文件中的段落color=#9cec1e;size=24px;[winter]color=#ec1ee5;size=56px;
# index.html{config_load file='smarty.conf' section='winter'} <!--引入配置文件--><style>body{color:{$smarty.config.color};font-size: {#size#}}</style><body><span>測(cè)試文本</span></body>
概念
運(yùn)算符
eq
相等(equal)neq
不等于(not equal)gt
大于(greater than)lt
小于(less than)lte
小于等于(less than or equal)gte
大于等于(great than or equal)is even
是偶數(shù)is odd
是奇數(shù)is not even
不是偶數(shù)is not odd
不是奇數(shù)not
非mod
求模取余div by
被整除is [not] div by
能否被某數(shù)整除{if $smarty.get.age is div by 3}...{/if}
is [not] even by
商的結(jié)果是否為偶數(shù)is [not] odd by
商的結(jié)果是否為奇數(shù)概念
語(yǔ)法
{if 條件}
{elseif 條件}
{else}
{/if}
# index.php<?phprequire './Smarty/Smarty.class.php';$smarty=new Smarty();$smarty->display('index.html');?>
# index.html{config_load file='smarty.conf' section='winter'} <!--引入配置文件--><style>body{color:{$smarty.config.color};font-size: {#size#}}</style><body>{if is_numeric($smarty.get.score)}{if $smarty.get.score gte 90}<span>A</span>{elseif $smarty.get.score gte 80}<span>B</span>{elseif $smarty.get.score gte 70}<span>C</span>{elseif $smarty.get.score gte 60}<span>D</span>{elseif $smarty.get.score lt 60}<span>E</span>{/if}{else}<span>不是數(shù)字</span>{/if}<hr>{if is_numeric($smarty.get.score)}{if $smarty.get.score is even}<span>是偶數(shù)</span>{elseif $smarty.get.score is odd}<span>是奇數(shù)</span>{/if}{/if}</body>
# index.php<?phprequire './Smarty/Smarty.class.php';$smarty=new Smarty();$stu= array('Sunny', 'Jerry');$emp= array('name'=>'Marry', 'sex'=>'girl');$goods= array( array('name'=>'ceilphone','price'=>2560), array('name'=>'notebook','price'=>3600));$smarty->assign('stu',$stu);$smarty->assign('emp',$emp);$smarty->assign('goods',$goods);$smarty->display('index.html');?>
# index.html{config_load file='smarty.conf'} <!--引入配置文件--><style>body{color:{$smarty.config.color};font-size: {#size#}}</style><body><div>學(xué)生:{$stu[0]} {$stu[1]}</div><div>雇員:{$emp['name']} {$emp.sex}</div><div>商品:</div><ul><li>{$goods[0]['name']}</li><li>{$goods[0].price}</li><li>{$goods[1]['name']}</li><li>{$goods[1].price}</li></ul></body>
{for} {while} {foreach} {section}
{foreach}
循環(huán){for 初始值 to 結(jié)束值 [step 步長(zhǎng)]} {/for}
# index.html<body>{for $i=0 to 10 step 2}<div>一江春水向東流</div>{/for}</body>
{while 條件} {/while}
# index.html<body>{$i=0}{while $i<5 }<div>{$i } 一江春水向東流</div>{/while}</body>
概念
語(yǔ)法
{foreach 數(shù)組 as $k=>$v}
{foreachelse}
{/foreach}
foreach的屬性
@index
從0開(kāi)始的索引@iteration
從1開(kāi)始的編號(hào)@first
是否是第一個(gè)元素@last
是否是最后一個(gè)元素# index.php<?phprequire './Smarty/Smarty.class.php';$smarty=new Smarty();$stu= array( 'first'=>'Sunny', 'second'=>'Jerry', 'third'=>'Marry', 'forth'=>'Tommy');$smarty->assign('stu',$stu);$smarty->display('index.html');?>
# index.html<body><table border='1' bordercolor='#000' width='780'><tr><th>是否是第一個(gè)元素</th><th>索引</th><th>編號(hào)</th><th>鍵</th><th>值</th><th>是否是最后一個(gè)元素</th></tr>{foreach $stu as $k=>$v}<tr><td>{if $v@first==1}<span>是第一個(gè)元素</span>{/if}</td><td>{$v@index}</td><td>{$v@iteration}</td><td>{$k}</td><td>{$v}</td><td>{if $v@last==1}<span>是最后一個(gè)元素</span>{/if}</td></tr>{foreachelse}沒(méi)有輸出{/foreach}</table></body>
概念
語(yǔ)法
{section name=自定義名字 loop=數(shù)組} {/section}
# index.php<?phprequire './Smarty/Smarty.class.php';$smarty=new Smarty();$stu= array('Sunny','Jerry','Marry','Tommy');$smarty->assign('stu',$stu);$smarty->display('index.html');?>
# index.html<body><table border='1' bordercolor='#000' width='780'><tr><th>是否是第一個(gè)元素</th><th>索引</th><th>編號(hào)</th><th>值</th><th>是否是最后一個(gè)元素</th></tr>{section name=s loop=$stu}<tr><td>{if $smarty.section.s.first ==1}是第一個(gè)元素{/if}</td><td>{$smarty.section.s.index}</td><td>{$smarty.section.s.iteration}</td><td>{$stu[s]}</td><td>{if $smarty.section.s.last==1}是最后一個(gè)元素{/if}</td></tr>{sectionelse}沒(méi)有輸出{/section}</table></body>
{$var=...}
變量賦值
{assign}
賦值
{while}
循環(huán)
{for}
循環(huán)
{foreach}
遍歷
{section}
遍歷數(shù)組
{function}
函數(shù)
{if}
條件
{include}
包含
{nocache}
禁止緩存
|
稱(chēng)為管道運(yùn)算符,將前面的參數(shù)傳遞后后面的修飾器使用# index.php<body>轉(zhuǎn)成大寫(xiě):{'abc'|upper} <br>轉(zhuǎn)成小寫(xiě):{'ABC'|lower} <br>默認(rèn)值:{$add|default:'地址不詳'}<br>去除標(biāo)簽:{'<b>你好嗎</b>'|strip_tags}<br>實(shí)體轉(zhuǎn)換:{'<b>你好嗎</b>'|escape}<br>日期:{$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}<br>多個(gè)管道連續(xù)使用:{'<b>boy</b>'|strip_tags|upper}<br></body>
概念
規(guī)則
例題
# modifier.cal.php<?phpfunction smarty_modifier_cal($num1,$num2,$num3){return $num1 $num2 $num3;}?>
# index.html{10|cal:20:30}
概念
方法
{literal} {/literal}
# index.html<style>{literal}body{color: #FF0000;}{/literal}</style>
$smarty->caching=true|1;
# index.php<?phprequire './Smarty/smarty.class.php';$smarty=new Smarty();$smarty->caching=true;//開(kāi)啟緩存if(date('H')>=9)$smarty->force_cache=true;//強(qiáng)制更新緩存$smarty->display('index.html');?>
$smarty->cache_lifetime=-1 | 0 | N
$smarty->cache_lifetime=3;//緩存的生命周期
{$變量名 nocache}
{nocache} {/nocache}
{$smarty.now nocache}
{nocache}{$smarty.now}{/nocache}
概念
語(yǔ)法
$smarty->display(模板,識(shí)別id)
# index.php<?phprequire './Smarty/smarty.class.php';$smarty=new Smarty();$smarty->caching=1;$smarty->display('index.html',$_GET['pageno']);?>
# index.html<body>這是第{$smarty.get.pageno}頁(yè)</body>
# index.php<?phprequire './Smarty/smarty.class.php';$smarty=new Smarty();$smarty->caching=1;$color=$_GET['color'];$size=$_GET['size'];$smarty->display('7-demo.html',"$color|$size");?>
# index.html<body>顏色:{$smarty.get.color}<br>大?。簕$smarty.get.size}<br></body>
$smarty->clearCache(模板,[識(shí)別id])
清除緩存$smarty->clearAllCache();
清除所有緩存# index.php<?phprequire './Smarty/smarty.class.php';$smarty=new Smarty();//$smarty->clearCache('7-demo.html',1);//$smarty->clearCache('7-demo.html','red|10');//$smarty->clearCache('7-demo.html');$smarty->clearAllCache();//清除所有緩存?>
Viewc
為混編目錄# Framework/Core/Framework.class.phpprivate static function initRoutes(){$p=$_GET['p']??$GLOBALS['config']['app']['dp'];$c=$_GET['c']??$GLOBALS['config']['app']['dc'];$a=$_GET['a']??$GLOBALS['config']['app']['da'];$p=ucfirst(strtolower($p));$c=ucfirst(strtolower($c));//首字母大寫(xiě)$a=strtolower($a);//轉(zhuǎn)成小寫(xiě)define('PLATFROM_NAME', $p); //平臺(tái)名常量define('CONTROLLER_NAME', $c); //控制器名常量define('ACTION_NAME', $a); //方法名常量define('__URL__', CONTROLLER_PATH.$p.DS); //當(dāng)前請(qǐng)求控制器的目錄地址define('__VIEW__',VIEW_PATH.$p.DS); //當(dāng)前視圖的目錄地址define('__VIEWC__', APP_PATH.'Viewc'.DS.$p.DS); //混編目錄}private static function initAutoLoad(){spl_autoload_register(function($class_name){//Smarty類(lèi)存儲(chǔ)不規(guī)則,所以將類(lèi)名和地址做一個(gè)映射$map=array('Smarty' => LIB_PATH.'Smarty'.DS.'Smarty.class.php');$namespace= dirname($class_name); //命名空間$class_name= basename($class_name); //類(lèi)名if(in_array($namespace, array('Core','Lib'))) //命名空間在Core和Lib下$path= FRAMEWORK_PATH.$namespace.DS.$class_name.'.class.php';elseif($namespace=='Model') //文件在Model下$path=MODEL_PATH.$class_name.'.class.php';elseif($namespace=='Traits') //文件在Traits下$path=TRAITS_PATH.$class_name.'.class.php';elseif(isset($map[$class_name]))$path=$map[$class_name];else //控制器$path=CONTROLLER_PATH.PLATFROM_NAME.DS.$class_name.'.class.php'; // $path=__URL__.$class_name.'.class.php';if(file_exists($path) && is_file($path))require $path;});}
# Framework/Core/Controller.class.php<?php//基礎(chǔ)控制器namespace Core;class Controller{ protected $smarty; use \Traits\Jump; public function __construct() { $this->initSession(); $this->initSmarty(); } //初始化session private function initSession(){ new \Lib\Session(); } //初始化Smarty private function initSmarty(){ $this->smarty=new \Smarty(); $this->smarty->setTemplateDir(__VIEW__); //設(shè)置模板目錄 $this->smarty->setCompileDir(__VIEWC__);//設(shè)置混編目錄 }}?>
# Application/Controller/Admin/ProductsController.class.phppublic function listAction(){// 實(shí)例化數(shù)據(jù)模型$model= new \Model\ProductsModel();$list= $model->select();// 加載視圖require __VIEW__.'products_list.html';$this->smarty->assign('list',$list);$this->smarty->display('products_list.html');}
# Application/View/Admin/products_list.html{foreach $list as $rows}<tr><td>{$rows['proID']}</td><td>{$rows['proname']}</td><td>{$rows['proprice']}</td><td><a href="index.php?p=Admin&c=Products&a=del&proid={$rows['proID']}" onclick="return confirm('確定要?jiǎng)h除嗎')">刪除</a></td><td><a href="index.php?p=Admin&c=Products&a=edit&proid={$rows['proID']}">修改</a></td></tr>{/foreach}
來(lái)源:https://www.icode9.com/content-1-795151.html
聯(lián)系客服