Smarty
![]() |
|
| Developer(s) | Monte Ohrt, Messju Mohr |
|---|---|
| Stable release | 3.1.13 / 16 January 2013 |
| Written in | PHP |
| Type | Template Engine |
| License | LGPL |
| Website | www.smarty.net |
Smarty is a web template system written in PHP. Smarty is primarily promoted as a tool for separation of concerns.[1] Smarty is intended to simplify compartmentalization, allowing the presentation of a web page to change separately from the back-end. Ideally, this eases the costs and efforts associated with software maintenance.
Smarty generates web content by the placement of special Smarty tags within a document. These tags are processed and substituted with other code. Tags are directives for Smarty that are enclosed by template delimiters. These directives can be variables, denoted by a dollar sign ($), functions, logical or loop statements. Smarty allows PHP programmers to define custom functions that can be accessed using Smarty tags.
Contents |
Smarty example [edit]
Since Smarty separates PHP from HTML, there are two files — one contains the presentation code: an HTML template, including Smarty variables and tags - {$title_text|escape} {$body_html} - which might look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html> <head> <title>{$title_text|escape}</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> </head> <body> {* This is a little comment that won't be visible in the HTML source *} {$body_html} </body><!-- this is a little comment that will be seen in the HTML source --> </html>
The business logic to use the Smarty template above could be as follows:
define('SMARTY_DIR', 'smarty-2.6.22/' ); require_once(SMARTY_DIR . 'Smarty.class.php'); $smarty = new Smarty(); $smarty->template_dir = './templates/'; $smarty->compile_dir = './templates/compile/'; $smarty->assign('title_text', 'TITLE: This is the Smarty basic example ...'); $smarty->assign('body_html', '<p>BODY: This is the message set using assign()</p>'); $smarty->display('index.tpl');
Further reading [edit]
- Hasin Hayder, J. P. Maia, Lucian Gheorghe (2006). Smarty PHP Template Programming And Applications. ISBN 978-1-904-81140-4.
References [edit]
- ^ Parr, Terence John (2004). Enforcing strict model-view separation in template engines. Proceedings of the 13th international conference on World Wide Web. ISBN 1-58113-844-X.
See also [edit]
External links [edit]
- Official website
- PHP Templating with Smarty by Cezar Floroiu - Smarty tutorial
