Smarty

Smarty is a free ( LGPL published ) template engine, which exists as a PHP library. It is designed with the aim to allow the development of web applications, the separation of code and output. The output is usually in HTML, is possible any text-based file format, for example, XML.

History

With the intention to create a simplified version of PHP for designers, developers created the Monte Orth and Andrei Zmievski 1999, the specification for a template engine, which was then programmed in the hope that they would find their way into PHP, C. The project was not integrated for various reasons in the PHP core. This was due to technical difficulties, an unclear specification about the scope and the fact that even an alternative syntax for templates has been integrated in the core of PHP.

By rejecting the project, the class SmartTemplate in which to parse regular expressions were used originated. This was never published. However, since the content of a site had to be parsed each time you call, resulted in a large overhead. To reduce this effect, Smarty was supplemented with the first publication on 18 January 2001 at precompilation of files ( caching).

Many well-known projects today focus on Smarty, such as the Framework Zikula, the Serendipity Blog or Forum Software WoltLab Burning Board.

Concept

The manufacturer describes Smarty as a " Template / Presentation Framework ." It is intended to provide extensive tools for solving many problems in the context of the " presentation layer " ( XHTML output ) of Web applications. With this concept, Smarty advanced as many other manufacturers also have the functions of a regular template engine system.

The developers of Smarty go in principle assume that the pure layout of a web application from a " template designer " designed and is being implemented in HTML ( as a template ), while the actual processes and data processing routines are developed by a programmer in PHP. It therefore seems useful to separate these two components in the development process. To this end, the development is divided into application logic processing PHP files, as well as templates that include the basic framework of the issue and are in a proprietary format. These template files are essentially HTML files with more or less complex placeholders that are later replaced with dynamic content.

In order to enable the templates a certain controllability, Smarty will recognize in addition to the template variables some simple commands, which were modeled on the PHP syntax, and the formulation of conditions (if) or loops ( foreach ) allow.

Smarty is optimized for professional use and contains a kind of compiler, which prepares the output of the Smarty templates and stores it in a cache. Thus, the output is accelerated.

As an open source program library Smarty can be extended and customized as desired.

Performance

Technical performance

Smarty processes the data and templates in two steps, which greatly speeds up the processing:

  • First, it is automatically PHP and XHTML code and transparent to the programmer a new PHP file is generated that contains both XHTML and PHP - that is, what would make a programmer without templates themselves otherwise. Only when changing a PHP file, the temporary PHP file is updated. This does not have to be re- parsed on every page request the template.
  • If necessary, Smarty, the final XHTML output caching (English ) by saving. This means that the web server in a convenient access returns a static XHTML page, which is especially database access in PHP has a massive relief of the server and the database result. The programmer only needs to make sure that after updating the database, the cache is cleared. Since databases are in many cases much more often queried as modified, this shall be regarded as a particular advantage.

From the first point, there is, however, that a Smarty - based output can never be faster than a pure PHP output, even in the best case ( without loops and in cached mode ).

Development speed

The rate of development of projects that use Smarty (or general template systems ), can be well above the pure PHP solutions. This is especially the case when programmers and designers come with large projects in the way of what may have processing bottlenecks result. Then allows for a clean separation of code and templates a division of labor, increase productivity effect generally on the overall project.

Whether to recognized development methodology for modularizing software, better results can be achieved with Smarty, seems questionable. The additional training time in the Smarty own language syntax, the development speed over other solutions may even fall.

Smarty in practice

Smarty can be easily integrated in PHP:

security = true; $ my_smarty -> secure_dir = ' / templates '; $ my_smarty -> compile_dir = ' / SMARTY_CACHE '; $ my_smarty -> left_delimiter = ' { .. '; $ my_smarty -> right_delimiter = ' .. }'; $ my_smarty -> debugging = false;? > After an object has been created, the directories for the templates and the cache must be specified. The code to be passed to the templates must be registered before:

assign ('name', $ name ); $ my_smarty -> assign (' first_name ', $ first_name );? > The Smarty templates themselves are simple XHTML files that can contain the Smarty Smarty variables and possibly control code ( especially loop logic) at the desired locations.

The following example demonstrates a Smarty template without control logic. The default delimiter for Smarty are { and }. However, it has been found in practice to be recommended to use other delimiters (see first PHP example ), as they may collide with the braces possibly embedded JavaScript.

Name: .. { $ name } .. First name: { $ first_name .. ..} This problem can be circumvented by the JavaScript with {literal } tags is masked.

Advantages of your own templating syntax

From the Manufacturer The following advantages are provided by a separate template engine:

  • The designer can cause the program code has no bugs or security issues, as they do not come into contact with the program code. Error within the Smarty syntax are intercepted by Smarty.
  • The designer can change the layout without the need for a programmer for it.
  • The programmer need not worry about the presentation of their data themselves.
  • The templates are very close to the actual output, ie they contain in many cases large parts of the final XHTML page.

Disadvantages and criticisms

PHP was even designed as a " template engine ": it is possible without expanding software libraries to perform text output efficiently and clearly with variables ( " wildcards "). The introduction of a specific language syntax as Smarty is therefore not absolutely necessary.

Disadvantages of the syntax are:

  • Increased training time ( learning curve) for the Developer
  • Reduced performance compared to a native PHP edition
  • Just the new language syntax is implemented, no MVC, this additional effort is required.
  • A Web page can be output incorrectly despite Smarty integration
  • IDE own syntax highlighting engage in the Smarty syntax does not - here would Extensions are installed
  • Smarty offered up to version 3 is no way to get a copy recursively
734697
de