smarty课程---smarty3的安装和使用
smarty課程---smarty3的安裝和使用
一、總結
一句話總結:smarty 是什么,就不多說了,用過php,接觸過php的人都對smarty 再熟悉不過了。它是一個很強大的代碼分離軟件,作為PHP的第三方類庫引用到PHP項目中,將PHP代碼和HTML完美分開,加速程序員和前端人員協同開發,提高開發速度。
代碼分離 php html 前端 后端
多看源代碼
?
1、smarty3如何安裝?
類庫 引入 new
我們前面說過,smarty是php的一個類庫導入到項目中,所以第一步,我們就要引入這個類庫。
?
require './libs/Smarty.class.php';
引入進來之后,那么就要new 一個smarty實例了。
?
?
//smarty 3 $smarty = new Smarty; $smarty->setTemplateDir('./tpl/'); $smarty->setCompileDir('./comp/');?
?
2、thinkphp采用smarty作為模板引擎,給我們什么啟示?
原生 使用
smarty里面的一些原生的操作方法,比如變量等等,都是可以在thinkphp中使用的,可以試試
不過一般用不著,現在提供的方法和功能已經可以滿足所有了
?
3、如果css 和js 的{ } 沒有換行寫,和smarty中的定界符{} 沖突怎么辦?
修改 定界符
修改smarty中的定界符
1 <html> 2 3 <head> 4 5 <title><{$title}></title> 6 7 <style type="text/css"> 8 body {width: 860px;margin: 0px;padding: 0px;} 9 </style> 10 11 <script type="text/javascript"> 12 function addStips(){alert(123);} 13 addStips(); 14 </script> 15 16 </head> 17 <body> 18 {$content} 19 </body> 20 21 </html>我們查看lib/Smarty.class.php 中我們發現定界符的定義
?
/*** template left-delimiter* @var string*/public $left_delimiter = "{";/*** template right-delimiter* @var string*/public $right_delimiter = "}";
默認的是{ } 極容易和css 還有js 的大括號沖突,所以我們要修改一下。
?
4、控制器分配模板的時候是使用絕對路徑還是相對路徑?
相對路徑
php:admin/admin.php
html:tpl/user/admin.html
所以我們總是理所當然的認為是這樣:
?
require '../init.class.php';$smarty->assign('title',1111);$smarty->assign('content',2222);$smarty->display('../user/addUser.html'); //錯的。
其實,是錯的,我們只需要記住:這個display永遠是相對于init.class.php中設定的tpl的路徑,永遠是和tpl/目錄的。所以不需要手動加../等跳轉目錄:
?
?
require '../init.class.php';$smarty->assign('title',1111);$smarty->assign('content',2222);$smarty->display('user/addUser.html'); //正確?
?
二、1.smarty3的安裝和使用
寫在前面:
smarty 是什么,就不多說了,用過php,接觸過php的人都對smarty 再熟悉不過了。它是一個很強大的代碼分離軟件,作為PHP的第三方類庫引用到PHP項目中,將PHP代碼和HTML完美分開,加速程序員和前端人員協同開發,提高開發速度。
?
1. 下載smarty
smarty 的目前最新版本是3版本。http://www.smarty.net/files/Smarty-3.1.14.zip
下載下來。解壓,我們需要是里面的libs 文件夾的內容。復制這個文件夾,到我們的文件的php項目中。
?
2. 使用smarty
我們前面說過,smarty是php的一個類庫導入到項目中,所以第一步,我們就要引入這個類庫。
?
require './libs/Smarty.class.php';引入進來之后,那么就要new 一個smarty實例了。
?
?
//smarty 3 $smarty = new Smarty; $smarty->setTemplateDir('./tpl/'); $smarty->setCompileDir('./comp/');?
對比一下smarty3 和 2 ,我們發現,smarty 3 完全改成php面向對象的方式來處理,我們看一下smarty 2 中是如何定義模板和緩存目錄的:
?
//smarty 2 $smarty = new Smarty(); //建立smarty實例對象$smarty $smarty->templates("./tpl/"); //設置模板目錄 $smarty->templates_c("./comp/"); //設置編譯目錄?
下面是完整的php代碼
require './libs/Smarty.class.php'; $smarty = new Smarty; $smarty->setTemplateDir(ROOT.'tpl/'); $smarty->setCompileDir(ROOT.'comp/'); $title = '這是一個smarty3的標題'; $content = '歡迎使用smarty 3 模版引擎!'; $smarty->assign('title',$title); $smarty->assign('content',$content); $smarty->display('index.html');?
html 代碼:
?
<html><head><title><{$title}></title></head><body><{$content}></body></html>允許之后是可以正常運行的。
?
?
3. smarty 的優化
在第2點中的基本設置,smarty 已經可以正常使用,但是,我們在使用中會發現幾個問題:
比如在index.html中有如下代碼:
?
<html><head><title><{$title}></title><style type="text/css">body {width: 860px;margin: 0px;padding: 0px;}</style><script type="text/javascript">function addStips(){alert(123);}addStips();</script></head><body>{$content} </body> </html>?
?
我們運行代碼的時候發現報錯了:
?
Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "D:\wamp\www\yangyi\2smarty3\tpl\index.html" on line 8 "body {width: 860px;margin: 0px;padding: 0px;}" - Unexpected ": ", expected one of: "}" , " " , ATTR' in D:\wamp\www\yangyi\2smarty3\libs\sysplugins\smarty_internal_templatecompilerbase.php on line 667?
我們仔細發現,原來css 和js 的{ } 沒有換行寫,和smarty中的定界符{} 沖突了,然后當作變量解析了,所以出錯。那如何修改這個問題呢。那就只有修改smarty的定界符了。
我們查看lib/Smarty.class.php 中我們發現定界符的定義
?
/*** template left-delimiter* @var string*/public $left_delimiter = "{";/*** template right-delimiter* @var string*/public $right_delimiter = "}";默認的是{ } 極容易和css 還有js 的大括號沖突,所以我們要修改一下。
$smarty->left_delimiter = '<{'; $smarty->right_delimiter = '}>';
接下來把html改一下:
?
?
<html><head><title><{$title}></title><style type="text/css">body {width: 860px;margin: 0px;padding: 0px;}</style><script type="text/javascript">function addStips(){alert(123);}addStips();</script></head><body><{$content}></body> </html>這樣就可以共存了,不要報錯誤。
?
?
4.smarty 的優化-配置文件導入
我們在項目過程中,是一定會有多個php文件,也可能有前臺和后臺,那么我們就要在每個php文件開頭都這樣來一次:
?
require './libs/Smarty.class.php'; $smarty = new Smarty; $smarty->setTemplateDir(ROOT.'tpl/'); $smarty->setCompileDir(ROOT.'comp/'); $smarty->left_delimiter = '<{'; $smarty->right_delimiter = '}>';這樣帶來的一個問題是很麻煩,代碼冗余度也太高了,所以我們可以寫在一個公共文件里面init.class.php,哪個文件需要smarty,引用進來不就可以了嘛:
?
?
//init.class.phprequire ROOT.'libs/Smarty.class.php';$smarty = new Smarty; $smarty->setTemplateDir('tpl/'); $smarty->setCompileDir('comp/'); $smarty->left_delimiter = '<{'; $smarty->right_delimiter = '}>';那么在inde.php中我們就可以這樣引用了:
?
?
require './init.class.php';$title = '這是一個smarty3的標題';$content = '歡迎使用smarty 3 模版引擎!';$smarty->assign('title',$title);$smarty->assign('content',$content);$smarty->display('index.html');?
5. smarty 的優化-分級目錄如何導入
上面寫在一個配置文件中之后,我們使用起來就很方便了,但是現在又出現另外一個問題,就是多目錄了,比如有一個admin/admin.php 中也需要smarty ,那么引入進來之后,卻發現報錯了,說找不到smarty.class.php
所以,我們必須在init.class.php中導入smarty時候目錄用絕對路徑就可以了:
?
$root = str_replace('\\', '/', __FILE__); define('ROOT', dirname($root).'/'); require ROOT.'libs/Smarty.class.php';$smarty = new Smarty; $smarty->setTemplateDir(ROOT.'tpl/'); $smarty->setCompileDir(ROOT.'comp/'); $smarty->left_delimiter = '<{'; $smarty->right_delimiter = '}>';這樣,這個init.class.php 文件就可以被任意項目中的目錄引用,都不會報錯了。
?
?
6.smarty 的優化-display()的路徑
我們經常發現一些人在用display的時候,總是寫不對,總是相對于當前的php文件來設定目錄:
?
php:admin/admin.php
html:tpl/user/admin.html
所以我們總是理所當然的認為是這樣:
?
require '../init.class.php';$smarty->assign('title',1111);$smarty->assign('content',2222);$smarty->display('../user/addUser.html'); //錯的。其實,是錯的,我們只需要記住:這個display永遠是相對于init.class.php中設定的tpl的路徑,永遠是和tpl/目錄的。所以不需要手動加../等跳轉目錄:
?
?
require '../init.class.php';$smarty->assign('title',1111);$smarty->assign('content',2222);$smarty->display('user/addUser.html'); //正確7.總結
多看源代碼,比如,我們忘記了如何設定模板和緩存模板,忘記了設定左定界符和右定界符符合寫。那么就可以打來Smarty.class.php 搜一下,多看看這個文件。就好辦了。
?
參考:1.smarty3的安裝和使用 - think2me - CSDN博客
https://blog.csdn.net/think2me/article/details/9320985
轉載于:https://www.cnblogs.com/Renyi-Fan/p/10117403.html
總結
以上是生活随笔為你收集整理的smarty课程---smarty3的安装和使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 工业4.0的小小思考
- 下一篇: Eugeny and Array(水题,