Smarty
Encyclopedia
Smarty is a web template system
Web template system
A Web template system describes the software and methodologies used to produce web pages and for deployment on websites and delivery over the Internet. Such systems process web templates, using a template engine...

 written in PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...

. Smarty is primarily promoted as a tool for separation of concerns
Separation of concerns
In computer science, separation of concerns is the process of separating a computer program into distinct features that overlap in functionality as little as possible. A concern is any piece of interest or focus in a program. Typically, concerns are synonymous with features or behaviors...

.
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
Software maintenance
Software Maintenance in software engineering is the modification of a software product after delivery to correct faults, to improve performance or other attributes....

.

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
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...

, denoted by a dollar sign ($), functions, logical
Conditional statement
In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false...

 or loop
Control flow
In computer science, control flow refers to the order in which the individual statements, instructions, or function calls of an imperative or a declarative program are executed or evaluated....

 statements. Smarty allows PHP programmers to define custom functions that can be accessed using Smarty tags.

Features

Smarty supports several high-level template programming features, including :
  • Control flow
    Control flow
    In computer science, control flow refers to the order in which the individual statements, instructions, or function calls of an imperative or a declarative program are executed or evaluated....

     statements, foreach
    Foreach
    For each is a computer language idiom for traversing items in a collection. Foreach is usually used in place of a standard for statement. Unlike other for loop constructs, however, foreach loops usually maintain no explicit counter: they essentially say "do this to everything in this set",...

  • if, elseif, else statements
  • variable modifiers - For example {$variable|nl2br}
  • functions - For example {html_select_date start_year='-5' end_year='+5'}
  • output filters
  • possibility to create own modifiers / functions / output filters
  • advanced caching of pages.
  • possibility to execute output filters.


along with other features. There are other template engines and frameworks that also support these features. Smarty templates can be incorporated into existing PHP web applications. It is used where a web application or a website has a theme system built into it, where the templates can be changed from theme to theme.

Criticism

Smarty has been criticized for replicating features that PHP does natively very well, which leads to inefficiency. Not only does Smarty require learning a new pseudo-language, it creates additional work for the processor. It is also seen as a poor replacement for full PHP frameworks, which have the benefits of Smarty without added complexity. Despite Smarty's flaws, including bugs and its superfluous nature within PHP frameworks, some developers still use it in conjunction with frameworks.

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




{$title_text|escape}



{* This is a little comment that won't be visible in the HTML source *}

{$body_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', '

BODY: This is the message set using assign

');

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

Further reading


External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK