PHP——smarty模板(第一天)
?
smarty.class.php 主要的東西放在類里面
templates 放模板
templates_c 放緩存
類里面 $smarty->assign("author","feng and meizi")
$smarty->display("test.tpl")
請求的是test.php 找的是test.tpl文件 test.tpl:<{$author}>
test.tpl文件將所有內容替換完成之后
<div><?php echo $this->tpl_vars["author"];?></div>
存在templates_c文件里的com_test.tpl.php
test.php 里面有個include()函數將頁面com_test.tpl.php包含進來
最后顯示的是feng and meizi
配置smarty模板
輸出配置文件的文件目錄
主文件為test.php
配置文件:init.inc.php
定義模板文件夾 templates test.html
定義臨時模板存放的文件夾 templates_c
添加模板擴充插件存放目錄 plugins
定義緩存文件存放目錄 cache
定義模板配置文件存放目錄 configs
自定義配置文件 configns文件夾里面的test.conf
系統自帶調節器 |truncate:5:"..." 需要三個參數:第一個參數為自身,不用寫
配置文件里用:$smarty->addPluginsDir(ROOT.'plugins/');
調節器是以modifier開頭的
命名規則:
function smarty_modifiercompiler_lower($params)
{
if (Smarty::$_MBSTRING) {
return 'mb_strtolower(' . $params[0] . ', \'' . addslashes(Smarty::$_CHARSET) . '\')';
}
// no MBString fallback
return 'strtolower(' . $params[0] . ')';
}
自定義插件 1.新建插件目錄
2.配置文件中添加插件目錄 $smarty->addPluginsDir(ROOT.'plugins/');
3.目錄下新建插件文件modifier.mystyle.php
4.寫函數
<?php
function smarty_modifier_mystyle($str,$color="yellow",$size="20")
{
$str = "<span style='color:".$color."; font-size:".$size."px'>".$str."</span>";
return $str;
}
?>
?Test.php
<?php //加載初始化文件 require "init.inc.php";//用assign()方法將變量植入到模板內$smarty->assign("title","測試網頁");$smarty->assign("content","內容"); $smarty->assign("neirong","this is a test demo");$smarty->assign("shuzu",array("one"=>1,"two"=>2,3,4,5));$smarty->assign("shijian",time());@$smarty->assign("get",$_GET);class Ren {public $name ="張三"; } $ren = new Ren();$smarty->assign("ren",$ren);//用smarty對象中的display()方法將網頁輸出 $smarty->display("Test.html");?>Test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><{$title}></title> </head><body><{$content}><br /> <{$neirong}><br /> <{print_r($shuzu)}><br /> <{$shuzu.one}><br /> <{$shuzu.two}> <{$ren->name}> <{$smarty.get.name}><!--引用自定義的配置文件--> <{config_load file="test.conf" section="two"}><div style="background-color:<{ #bg# }>; width:100px; height:100px; font-size:<{#size#}>px">好厲害</div> </body><!--cat連接字符串--> <div><{$neirong|cat:"中國"|cat:"china"}></div><!--截取英文的時候按單詞來截,中文不存在這種情況--> <div><{$neirong|truncate:6:""}></div><!--這樣就可以分開截英文--> <div><{$neirong|truncate:6:true}></div><!--處理時間函數--><!--default設置默認值--><!--調用自定義的調節器--> <div><{$shijian|date_format:"%Y-%m-%d %H:%M:%S "}></div><div><{$aa|default:"hello"}></div><div><{$neirong|mystyle:green:50}></div></html>init.inc.php
<?phpdefine("ROOT",str_replace("\\","/",dirname(__FILE__)).'/'); //常量ROOT中指定項目根目錄require ROOT.'libs/Smarty.class.php'; //加載Smarty類文件$smarty = new Smarty(); //實例化Smarty對象<br>$smarty -> auto_literal = false; //就可以讓定界符號使用空格 $smarty->setTemplateDir(ROOT.'templates/'); //設置所有模板文件存放位置 //$smarty->addTemplateDir(ROOT.'templates2/'); //添加一個模板文件夾 $smarty->setCompileDir(ROOT.'templates_c/'); //設置編譯過的模板存放的目錄 $smarty->addPluginsDir(ROOT.'plugins/'); //設置為模板擴充插件存放目錄 $smarty->setCacheDir(ROOT.'cache/'); //設置緩存文件存放目錄 $smarty->setConfigDir(ROOT.'configs'); //設置模板配置文件存放目錄$smarty->caching = false; //設置Smarty緩存開關功能 $smarty->cache_lifetime = 60*60*24; //設置緩存模板有效時間一天 $smarty->left_delimiter = '<{'; //設置模板語言中的左結束符 $smarty->right_delimiter = '}>'; //設置模板語言中的右結束符?>modifier.mystyle.php
<?phpfunction smarty_modifier_mystyle($str,$color="yellow",$size="20") {$str = "<span style='color:".$color."; font-size:".$size."px'>".$str."</span>";return $str; }?>test.conf
[one] bg=red size=50[two] bg=yellow size=24?
?
轉載于:https://www.cnblogs.com/Chenshuai7/p/5262236.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的PHP——smarty模板(第一天)的全部內容,希望文章能夠幫你解決所遇到的問題。