smarty初步應用

1.從smarty官網上下載最新的版本文件  :  https://www.smarty.net/download

我目前下載的最新版本是3.1.33

2.在自己網站根目錄創建一個smarty文件夾,將下載的壓縮包解壓,複製 libs 文件夾到該目錄下

libs爲smarty的核心文件無需更改,demo爲示例,事後可以看一下(一下是文件夾的目錄)

3.現在我們在smarty(核心文件)的同級目錄創建一個test文件夾     文件夾下創建test.php文件 (內容如下:)

<?php
	//smarty引入
	require('../smarty/Smarty.class.php');   //對比自己核心文件目錄(我的目錄是smarty)

	//實例化
	$smarty = new Smarty();

	$smarty->left_delimiter = "{"; 				    //左定界符
	$smarty->right_delimiter = "}";				    //右定界符
	$smarty->template_dir = "templates/templates";	//html模板地址
	$smarty->compile_dir = "templates/templates_c"; //模板編譯生成文件
	$smarty->config_dir = "templates/config";		//模板配置
	$smarty->cache_dir = "templates/cache";			//緩存
	//smarty模板有高速緩存的功能,如果這裏是true的話即打開caching,但是會造成網頁不立即更新的問題,當然也可以通過其他的辦法解決
	//$smarty->caching = false;
	//$smarty->cache_lifettime = 120; //緩存時間

	$smarty->assign('title','文章標題');
	//指向文件
	$smarty->display('../template/templates/test.tpl');
?>

根據test.php在smarty同級下創建template文件夾   template文件夾下有該文件夾

在templates下創建一個測試文件   名爲test.tpl文件  (文件後綴隨意命名,我這兒定義後綴爲tpl,代表是模板文件)


{$title} <br/>   

test.tpl  文件 寫入  {$title}   爲模板引擎定義的輸出方式  { }爲左右定界符   

運行文件輸出文章標題

 

自此smarty初步使用已完成,快去試一下吧!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章