日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SMARTY的知识

發布時間:2024/6/21 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SMARTY的知识 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

smarty的原理:

<?php class Smarty {$ldelimiter = "{";//左分隔符$rdelimiter = "}";//友分隔符$attr = array();//用來存貯變量的數組//向模板里面注冊變量function assign($key,$value){$this->attr[$key] = $value;}//顯示模板的方法function display($url){//1.獲取靜態模板的內容$str = file_get_contents($url);//2.根據正則匹配str里面出現的所有{}里面的內容//{$a} -> <?php echo $attr[$a] ?>//3.將替換好的頁面保存臨時文件touch(".test.php");file_put_contents();//4.將臨時文件拿到當前頁面顯示include(".test.php");} }

訪問的是PHP文件,其中路徑就按照訪問的PHP文件來定。但是顯示的是html。

test.php

test.php <?php include("../init.inc.php"); //注冊變量 $smarty->assign("name","張三"); $smarty->assign("arr",array(1,2,3,4,5)); $smarty->assign("test","my name is zhangsan"); $smarty->assign("title",啊啊啊啊啊啊); //顯示模板 $smarty->display("test.html");

test.html

<html><head></head><body><{$name}><{$arr.one}><{config_load file='test.conf'} section='one'><div style="width:20px;height:20px;background-color:<{#color#}>"></div><{$smarty.now|date_format:%Y-%m-%d %H:%M:%S}><{$test|truncate:5}>//截取字符串
    

     <{date name ="riqi" value="2017-2-3" time=true}>
     <{color name="color"}>
     <{textarea name="txt" toolbar="full" color="red"}>
     <{/textarea}>//塊函數要有首和尾
     
     <{cishu num=3}>
     <{font da=50}>
     電話卡會打架
     <{/font}>
     
</body> </html>$smarty.const.XXX 取常量 $smarty.config.color 和 #color#一樣

test.conf

[one] color=red [two] color=green

自定義變量調節器modifier.keyword.php

<?php function smarty_modifier_keyword($str,$key) {return str_replace($key,"<mark>{$key}</mark>",$str); }

自定義函數function.cishu.php

//num參數代表輸出次數 function smarty_function_cishu($args,$smarty) {$num = $args["num"];$str = "";for($i=0;$i<$num;$i++){$str = $str."<div style='width:100px;height:100px;background-color:green'></div>";}return $str; }

自定義塊函數block.font.php

<?php function smarty_block_font($args,$content,$smarty,$a) {if(!$a)//走頭的時候沒有內容,走尾的時候把內容改變{$da = $args["da"];return "<span style='font-size:{$da}px'>{$content}</span>";} }

?

顯示結果

?

?

?

?

?

轉載于:https://www.cnblogs.com/gaobint/p/6769314.html

總結

以上是生活随笔為你收集整理的SMARTY的知识的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。