smarty小記,smarty_PHP教程

來源:互聯網
上載者:User

smarty小記,smarty


項目中經常用到smarty,但是一直用的就是簡單的 assgin display foreach if else 等幾個方法。 閑下來又看了一遍smarty。簡單的記錄下有用的東西。


smarty 系統保留變數   $smarty.get #使用方法 $smarty.get.id 下面的一次類推   $smarty.post   $smarty.session   $smarty.cookie   $smarty.server

smarty模板中使用PHP中定義的常量
php 檔案中定義 define('HIEGHT', 8846);
模板檔案中使用 $smarty.const.HIEGHT

smarty讀取設定檔中的變數
1 $smarty->config_dir = "./conf"; #設定設定檔的路徑
2 設定檔的書寫規則 1)設定檔以.conf 結尾 2)設定檔內的書寫規則 key1=val1 key1=val1
3 使用:在模板檔案的頭部 {config_load file='site.conf'}
4 {$smarty.config.site} 即可擷取 // {#site#}也可以讀取

smarty緩衝設定
  $smarty->caching = true; #開啟緩衝 快取頁面面
  $smarty->cache_lifetime = 3600; #緩衝的時間
  $smarty->cache_dir = './cache'; #存放換檔案的目錄
$smarty->isCached('news.html') 可以判斷是否產生了快取檔案 #smarty3裡面這麼判斷

#一個頁面產生多個快取檔案 例子:一個商品詳情的頁面 根據商品的ID 展現不同的內容
$id = empty($_GET['id]) ? 1 : $_GET['id'];
$good_name = 'good_name_' . $id;
$smarty->assign('good_name',$good_name);
$smarty->display('tpm.html',$id); # 這樣就會根據id產生不同的頁面 。
注意: 實際的項目中 這個ID 應該是根據參數算出來的。例如根據page,category等參數計算出來。之前公司的一個項目用到了smarty的緩衝,這個id的值就是根據page    category md5以後得到的。 ecshop中也是這麼做的 不過不是MD5。

#清除緩衝
$smarty->clearCache('tmp_name')
$smarty->clearAllCache()

#設定局部不緩衝的方法
1 在標籤中 {$time|date_format:"%Y-%M-%D %H:%M:%S" nocache}
2 {nocache}{/nocache} 控制局部不緩衝
3 php賦值的時候控制不緩衝 assign 的第三個參數 設定為true
4 smarty中的一個insert方法 不細說 感覺沒啥用 (囧)
   ######個人感覺smarty的緩衝意義不太大。因為完全可以用redis MEMCHACHE 代替。當然存在即合理 哈哈 



#變數調節器 :簡單說 修改變數的展示形式的功能 例如:
   {$time|date_format:"%Y-%M-%D %H:%M:%S" nocache}

#自己也可以寫變數調節器 參照plugin裡面的檔案寫就可以了 ###感覺有用。我們的項目中用到了。具體的使用,回頭補上。


   smarty擷取:

1 download: http://www.smarty.net/download
2 通過composer擷取 : composer是個很不錯的東西。




http://www.bkjia.com/PHPjc/923717.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/923717.htmlTechArticlesmarty小記,smarty 項目中經常用到smarty,但是一直用的就是簡單的 assgin display foreach if else 等幾個方法。 閑下來又看了一遍smarty。簡單的記錄...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.