Smarty

From Free net encyclopedia

Smarty is a template engine written in PHP. Smarty separates PHP from HTML and is primarily used for the generation of dynamic HTML content, by allowing for the placement of special Smarty tags within a document. Allowable tags include, but are not limited to, variables such as {$variable}, and a range of logical and loop operators.

Smarty is most often used by PHP-based applications to separate "business logic" from presentation. This makes for much cleaner coding and more flexible modification. For instance, one could typically modify a Smarty-based application's appearance (or, skin) easily, without ever looking at any business logic or PHP code.

Smarty allows template programming with

and many more features. Other template engines also support these commands in templates.

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 HTML source *}

<p>{$body_text}</p>

</body><!-- this is a little comment that will be seen in 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->config_dir = SMARTY_DIR;
$smarty->template_dir = './tmpl';
$smarty->compile_dir = './tmpl/compile';
$smarty->compile_check = TRUE;
$smarty->debugging = FALSE;

$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('basic.htm');

Software built with Smarty

Smarty official resource page.

External links


Template:Compu-soft-stubde:Smarty es:Smarty id:Smarty ru:Smarty fi:Smarty