Smarty is a well known template engine for PHP developments. Smarty’s main concern is to separate the core PHP code from the design layout and files, including CSS style sheets, actual HTML templates etc.
Smarty uses its own tags structure in the HTML pages and optimize the overall website speed and performance by utilizing its built-in cache. Smarty has a vast range of predefined tags to be used in HTML web page. It also allows us an expert PHP programmer to develop its plugin and call them when needed in the code.
Smarty is being used by several popular websites including Flickr, allthewebsites.org and thousands of other sites.
Smarty supports several high-level template programming features, including
An HTML template with some basic Smarty tags would look like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>{$PageTitle}</title> </head> <body> {* This is a little comment that won't be visible in the HTML source *} {$PageBody} {* Page html body and all the design layout here*} </body> </html> |
The PHP code for the above Smarty Template would look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php define('SMARTY_DIR', 'smarty/' ); require_once(SMARTY_DIR . 'Smarty.class.php'); $smarty = new Smarty(); $smarty->template_dir = 'templates/default'; $smarty->compile_dir = 'templates/default/compile/'; $smarty->assign('PageTitle', 'This is a basic example of Smarty Template Page'); $smarty->assign('PageBody', ' This is the text message set using assign() code from PHP '); $smarty->display('index.tpl'); ?> |