Smarty

from Wikipedia, the free encyclopedia
Smarty
Basic data

developer Monte Ohrt, Messju Mohr
Publishing year 2002
Current  version 3.1.35
( April 13, 2020 )
operating system platform independent
programming language PHP
License LGPL ( Free Software )
German speaking Yes
www.smarty.net

Smarty is a free (under the LGPL published) template engine , as PHP - Library present. It was designed with the aim of enabling the separation of code and output when developing web applications . The output is mostly in HTML , but any text-based file format is possible, for example XML .

concept

The manufacturer calls Smarty a "Template / Presentation Framework". It is intended to provide extensive tools for solving many problems in the area of ​​the "presentation layer" (XHTML output) of web applications. With this concept, Smarty, like many other manufacturers, expanded the functions of a regular template engine system.

The developers of Smarty basically assume that the pure layout of a web application is designed by a "template designer" and implemented in HTML (as a template), while the actual processes and data processing routines are developed by a programmer in PHP. It therefore makes sense to separate these two components during the development process. For this purpose, the development is divided into PHP files that process application logic, as well as templates that contain the basic structure of the output and are available in their own format. These template files are essentially HTML files with more or less complex placeholders that are later replaced by dynamic content.

In order to enable a certain degree of controllability in the templates, Smarty recognizes, in addition to the template variables, some simple control commands that are based on the PHP syntax and allow the formulation of conditions ( if) or loops ( foreach).

Smarty has been optimized for professional use and contains a kind of compiler that prepares the output of the Smarty templates and saves them in a cache . This speeds up the output.

As an open source program library, Smarty can be expanded and adapted as required.

syntax

Smarty can be integrated into PHP as follows:

<?php
require_once('/smarty/libs/Smarty.class.php');
$my_smarty = new Smarty();
$my_smarty->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 that is to be transferred to the templates must be registered beforehand:

<?php
$my_smarty->assign('name', $name);
$my_smarty->assign('first_name', $first_name);
?>

The Smarty templates themselves are simple XHTML files that can contain the Smarty variables and, if necessary, Smarty control code (in particular loop logic) at the desired locations.

The following example demonstrates a Smarty template without control logic . The default limiters for Smarty are {and}. In practice, however, it has turned out to be advisable to use other delimiters (see first PHP example), as the curly brackets could collide with any embedded JavaScript .

Name: {.. $name ..}
Vorname: {.. $first_name ..}

The workaround is to mask the JavaScript with {literal} tags.

history

With the intention of creating a simplified form of PHP for designers, the developers Monte Orth and Andrei Zmievski created the specification for a template engine in 1999, which was then programmed in C in the hope that it would find its way into PHP . However, the project was not integrated into the PHP core. The reasons for this were technical difficulties, an unclear specification about the scope of services and the fact that an alternative syntax for templates was integrated in the PHP core itself.

When the project was rejected, the SmartTemplate class was created, which uses regular expressions for parsing . However, this was never published. Since the content of a page had to be parsed each time it was called up, there was a large overhead . In order to reduce this effect, Smarty was supplemented with precompilation of files (caching) when it was first published on January 18, 2001.

Many well-known products rely on Smarty, such as the Serendipity weblog or the Shopware shop system . Other projects have meanwhile replaced Smarty with other template engines, for example the Zikula framework migrated to Twig. Still other projects designed their own template languages ​​inspired by Smarty, such as the forum software WoltLab Burning Board , which was once very widespread on the web .

literature

  • Joao Prado Maia, Hasin Hayder, Lucian Gheorghe: Smarty PHP Template Programming and Applications . Packt, 2006, ISBN 1-904811-40-X (English).
  • George Schlossnagle: Smarty . In: Professional PHP 5 Programming . Addison-Wesley, Munich 2006, ISBN 978-3-8273-2381-1 , pp. 136-149 .
  • Florence Maurice: Template engines using the example of Smarty . In: PHP 5.3 + MySQL 5.1 - Getting started with programming dynamic websites . Addison-Wesley, Munich 2009, ISBN 978-3-8273-2723-9 , pp. 415-428 .

Web links

Individual evidence

  1. Release 3.1.35 . April 13, 2020 (accessed April 14, 2020).
  2. https://docs.s9y.org/docs/developers/themes.html
  3. https://developers.shopware.com/designers-guide/smarty/
  4. https://zikula.readthedocs.io/en/stable/CHANGELOG-1.4/
  5. https://docs.woltlab.com/view_templates.html