Smarty
![]() | |
| éÂÂç¼è | Monte Ohrt, Messju Mohr |
|---|---|
| å½ÂÃ¥ÂÂçÂÂæÂ¬ | 3.1.27ï¼Â2015å¹´6æÂÂ19æÂÂ¥[1]ï¼ |
| æºÂ代ç Â庠| |
| ç±»å | ç¶²é Â模æÂ¿å¼ÂæÂ |
| 许å¯åÂÂè®® | LGPL |
| ç½Âç« | smarty.net |
SmartyæÂ¯ä¸ÂÃ¥ÂÂPHPä¸ÂçÂÂç¶²é Â模æÂ¿ç³»çµ±ãÂÂSmartyåºæÂ¬ä¸ÂæÂ¯ä¸Â種çºäºÂå°Âä¸ÂÃ¥ÂÂèÂÂéÂÂçÂÂäºÂæÂ åÂÂé¢èÂÂæÂ¨åºçÂÂ工堷ï¼ÂéÂÂå°ÂæÂÂäºÂæÂÂç¨ç¨Âå¼ÂæÂ¯ä¸Â種報éÂÂæÂ§è¨Âè¨ÂçÂÂçÂ¥ãÂÂ[2][3]
ç°¡ä»Â
Smarty以卿ÂÂä»¶ä¸ÂæÂ¾ç½®ç¹æ®ÂçÂÂãÂÂSmartyæ¨Â籤ãÂÂä¾Âç¢çÂÂç¶²é Â堧容ãÂÂéÂÂäºÂæ¨Â籤æÂÂ被èÂÂçÂÂ並æÂ¿æÂÂæÂÂå ¶ä»ÂçÂÂ堧容ãÂÂ
æ¨Â籤æÂ¯çµ¦SmartyçÂÂæÂÂ令符ï¼Â以模æÂ¿å®ÂçÂÂ符å ä½ÂãÂÂéÂÂäºÂæÂÂ令符å¯以æÂ¯è®ÂæÂ¸ï¼Â以$符èÂÂ代表å½æÂ¸ãÂÂéÂÂ輯æÂ æµÂç¨ÂæÂ§å¶èªÂæ³ÂãÂÂSmartyå Â許PHPç¨Âå¼Âè¨Âè¨Â師以Smartyæ¨Â籤å»å®Â義å¯åÂÂÃ¥ÂÂçÂÂå½æÂ¸ãÂÂ
SmartyæÂÂÃ¥ÂÂç°¡åÂÂÃ¥ÂÂÃ¥ÂÂÃ¥ÂÂï¼Âå Â許PHPç¶²é Âå¾Â端éÂÂ輯èÂÂ表ç¾層ï¼Âå³使ç¨è ä»Âé¢ï¼ÂÃ¥ÂÂé¢ãÂÂçÂÂæÂ³çÂÂæÂ æ³Âä¸Âï¼ÂéÂÂå°ÂéÂÂä½Âè»Âé«Âç¶Âè·費ç¨åÂÂ人åÂÂãÂÂå¨éÂÂÃ¥ÂÂç Âç¼çÂÂçÂ¥ä¹Âä¸Âï¼Âè¨Âè¨Â師å¯å°Â注æÂ¼å¯¦ç¾表ç¾層èÂÂä¸Âç¨æÂ°å¯«PHPç¨Âå¼Â碼ï¼Â並å Â許PHPç¨Âå¼Âè¨Âè¨Â師æÂ½é¢åº表ç¾層並å°Â注實ç¾å¾Â端éÂÂ輯ãÂÂ
SmartyæÂ¯æÂ´å¹¾åÂÂé«ÂéÂÂ模æÂ¿ç¨Âå¼ÂçÂÂç¹æÂ§ï¼Âå å«ï¼Â
- æÂ£è¦Â表示æ³Â
- æµÂç¨ÂæÂ§å¶èªÂæ³Âï¼Âå¦ÂforeachãÂÂwhileãÂÂ
- ifï¼Âelseifï¼Âelse
- å¯修æÂ¹çÂÂè®ÂæÂ¸ - ä¾Âå¦Â{$variable|nl2br}
- 使ç¨è èªè¨ÂçÂÂå½æÂ¸
- å¨模æÂ¿å §çÂÂæÂ¸å¸è¨Âç®Â
以åÂÂå ¶ä»Âç¹æÂ§ãÂÂä¸ÂäºÂå ¶ä»ÂçÂÂ模æÂ¿å¼ÂæÂÂä¹ÂæÂ¯æÂ´éÂÂé¡Âç¹æÂ§ãÂÂ
ç¨Âå¼Â碼ç¯Âä¾Â
å çº Smartyå° HTML碼èÂÂPHPç¨Âå¼Â碼åÂÂé¢ï¼ÂæÂÂ以æÂÂæÂÂå ©åÂÂæªÂæ¡Âï¼Â
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>{$title_text}</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body> {* This is a little comment that won't be visible in the HTML source *}
<p>{$body_text}</p>
</body><!-- this is a little comment that will be seen in the HTML source -->
</html>
å¨ä¼Âæ¥ÂéÂÂ輯ç¨Âå¼Â碼ä¸Âï¼Âè¨Âè¨Âè è½é Âç½®Smartyå»使ç¨éÂÂÃ¥ÂÂ模æÂ¿ï¼Â
define('SMARTY_DIR', 'smarty-2.6.9/' );
require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = './templates/';
$smarty->compile_dir = './templates/compile/';
$smarty->cache_dir = './templates/cache/';
$smarty->caching = false;
$smarty->error_reporting = E_ALL; // LEAVE E_ALL DURING DEVELOPMENT
$smarty->debugging = true;
$smarty->assign('title_text', 'TITLE: This is the Smarty basic example ...');
$smarty->assign('body_text', 'BODY: This is the message set using assign()');
$smarty->display('index.tpl');
並ä¸Âè½å°ÂPHPå 涵å¨Smarty模æÂ¿è£¡ï¼ÂÃ¥ÂÂæÂ¯ä¸Âé¢éÂÂ樣ï¼Â
{* We can use PHP syntax in smarty template inside {php} ..{/php} *}
{php}
$contentType = strpos($_SERVER['HTTP_ACCEPT']ï¼Â'application/xhtml+xml') === false ? 'text/html' : 'application/xhtml+xml';
header("Content-Type: $contentType; charset=utf-8");
{/php}
<html>
<head>
<title>{$page_title}</title>
</head>
<body>
{$body_text}
</body>
</html>
Ã¥ÂÂç §
- ^ Ã¥ÂÂæ¡£å¯æÂ¬. [2015-08-07]. ï¼ÂÃ¥ÂÂå§Âå 容åÂÂæ¡£äºÂ2019-02-17ï¼Â.
- ^ Smartyå°ÂPHP碼ï¼ÂäºÂÃ¥ÂÂéÂÂ輯ï¼Âè HTML碼åÂÂéÂÂï¼ÂéÂÂ常代表表ç¾éÂÂ輯ï¼Â.
- ^ Parr, Terence John. Enforcing strict model-view separation in template engines. Proceedings of the 13th international conference on World Wide Web. 2004. 1-58113-844-X.
Ã¥ÂÂè¦Â
å¤Âé¨é£çµÂ
è±æÂÂ
- Official siteï¼Â页é¢åÂÂæ¡£å¤Â份ï¼ÂÃ¥ÂÂäºÂäºÂèÂÂç½Âæ¡£æ¡Âé¦Âï¼Â
- PHP Templating with Smarty by Cezar Floroiu - Smarty tutorial
- Smarty vs. XML/XSLTï¼Â页é¢åÂÂæ¡£å¤Â份ï¼ÂÃ¥ÂÂäºÂäºÂèÂÂç½Âæ¡£æ¡Âé¦Âï¼ - from DevPapers.com by Sergey Makogon
- Smarty Cheat Sheetï¼Â页é¢åÂÂæ¡£å¤Â份ï¼ÂÃ¥ÂÂäºÂäºÂèÂÂç½Âæ¡£æ¡Âé¦Âï¼ Smarty Cheat Sheet for Templates Designers and Programmers
- Timestretch: PHP, MySQL, and Smarty Programming - Also see the PHP2 page for more.
- PHP Smarty Toolsï¼Â页é¢åÂÂæ¡£å¤Â份ï¼ÂÃ¥ÂÂäºÂäºÂèÂÂç½Âæ¡£æ¡Âé¦Âï¼ - Using Smarty in PhpED
