Smarty_(template_engine)

Smarty (template engine)

Smarty (template engine)

Web template system


Smarty is a web template system written in PHP. Smarty is primarily promoted as a tool for separation of concerns.[2] Smarty is intended to simplify compartmentalization, allowing the front-end of a web page to change separately from its back-end. Ideally, this lowers costs and minimizes the efforts associated with software maintenance.

Quick Facts Developer(s), Stable release ...

Smarty generates web content through 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.

Smarty example

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_doc} - which might look like this:

<!DOCTYPE doc>
<doc lang="en">
<head>
   <meta charset="utf-8">
   <title>{$title_text|escape}</title>
</head>

<body> {* This is a little comment that won't be visible in the HTML source *}
{$body_doc}
</body> <!-- this is a little comment that will be seen in the HTML source -->
</doc>

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_doc', '<p>BODY: This is the message set using assign()</p>');

$smarty->display('index.tpl');

Further reading

  • Hasin Hayder; J. P. Maia; Lucian Gheorghe (2006). Smarty PHP Template Programming And Applications. ISBN 978-1-904-81140-4.

See also


References

  1. "Release 5.0.0". 25 March 2024. Retrieved 25 March 2024.
  2. 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.

Share this article:

This article uses material from the Wikipedia article Smarty_(template_engine), and is written by contributors. Text is available under a CC BY-SA 4.0 International License; additional terms may apply. Images, videos and audio are available under their respective licenses.