Smarty
From Wikipedia, the free encyclopedia
Smarty | |
![]() |
|
Developer: | Monte Ohrt, Messju Mohr |
---|---|
Latest release: | 2.6.18 / 7th Mar 2007 |
Use: | Template Engine |
License: | LGPL |
Website: | smarty.php.net |
Smarty is a web template system written in PHP. Smarty separates PHP, as a business logic, from HTML, a presentation logic, and generates web content by the placement of special Smarty tags within a document (i.e. variable substitution).
Tags are directives for Smarty that are enclosed by delimiters. These directives can be variables, denoted by a dollar sign ($), functions, or logical or control flow statements. Smarty allows PHP programmers to define functions that can be accessed using Smarty tags.
The compartmentalization created by Smarty allows the presentation of a web page to change separately from the back-end, thus allowing applications to be developed in a more organized fashion. Using this development model, designers are hidden from the back-end coding and PHP programmers are hidden from the presentation coding.
Smarty allows template programming with several built in features, like:
- regular expressions
- foreach, while
- if, elseif, else
- variable modifiers - For example {$variable|nl2br}
- user created functions
- mathematical evaluation within the template
along with other features. There are other template engines that also support these features.
[edit] Code example
Since Smarty separates PHP from HTML, you have two files:
<!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>
In the business logic code you can configure Smarty to use this template:
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->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');
[edit] See also
[edit] External links
- Official site
- PHP Templating with Smarty by Cezar Floroiu - Smarty tutorial
- Smarty vs. XML/XSLT - from DevPapers.com by Sergey Makogon
- Article "Templates, Gift from Heaven or Syntactic Junk Food?" by Nathan Oostendorp
- 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