Smarty
Encyclopedia
Smarty is a web template system
written in PHP
. Smarty is primarily promoted as a tool for separation of concerns
.
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.
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.
The business logic to use the Smarty template above could be as follows:
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 flowControl flowIn 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, foreachForeachFor 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:The business logic to use the Smarty template above could be as follows:
Further reading
External links
- Official website
- PHP Templating with Smarty by Cezar Floroiu - Smarty tutorial
- Smarty vs. XML/XSLT - from DevPapers.com by Sergey Makogon
- Smarty Cheat Sheet Smarty Cheat Sheet for Templates Designers and Programmers
- Timestretch: PHP, MySQL, and Smarty Programming - Also see the PHP2 page for more.