Smarty (template engine)

Last updated
Smarty
Smarty-logo.png
Developer(s) Monte Ohrt, Messju Mohr, Uwe Tews
Stable release
3.1.34 / 5 November 2019;6 months ago (2019-11-05) [1]
Repository OOjs UI icon edit-ltr-progressive.svg
Written in PHP
Type Template Engine
License LGPL
Website www.smarty.net

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.

Contents

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

<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><title>{$title_text|escape}</title></head><body>{* This is a little comment that won't be visible in the HTML source *}{$body_html}</body><!-- this is a little comment that will be seen in the HTML source --></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=newSmarty();$smarty->template_dir='./templates/';$smarty->compile_dir='./templates/compile/';$smarty->assign('title_text','TITLE: This is the Smarty basic example ...');$smarty->assign('body_html','<p>BODY: This is the message set using assign()</p>');$smarty->display('index.tpl');

Further reading

See also

Related Research Articles

PHP is a popular general-purpose scripting language that is especially suited to web development. It was originally created by Rasmus Lerdorf in 1994; the PHP reference implementation is now produced by The PHP Group. PHP originally stood for Personal Home Page, but it now stands for the recursive initialism PHP: Hypertext Preprocessor.

JavaServer Pages (JSP) is a collection of technologies that helps software developers create dynamically generated web pages based on HTML, XML, SOAP, or other document types. Released in 1999 by Sun Microsystems, JSP is similar to PHP and ASP, but uses the Java programming language.

Quanta Plus

Quanta Plus, originally called Quanta, is a web Integrated development environment (IDE) for HTML, XHTML, CSS, XML, PHP and any other XML-based languages or scripting languages. Quanta was licensed under GPL before the release of version 2.0 final.

ColdFusion Markup Language, more commonly known as CFML, is a scripting language for web development that runs on the JVM, the .NET framework, and Google App Engine. Multiple commercial and open source implementations of CFML engines are available, including Adobe ColdFusion, Lucee, New Atlanta BlueDragon, Railo, and Open BlueDragon as well as other CFML server engines.

Javadoc is a documentation generator created by Sun Microsystems for the Java language for generating API documentation in HTML format from Java source code. The HTML format is used for adding the convenience of being able to hyperlink related documents together.

Open Power Template is a web template engine written in PHP 5. A common strategy in designing web application is separation of the application logic from the presentation. OPT is a tool for implementing such separation. The presentation layer is represented by templates, text files with HTML code and extra instructions controlling the data substitution.

vlibTemplate is a template engine written in PHP. Programmers and web developers may use it for web development. vlibTemplate is a PHP class that is intended to make splitting PHP from HTML a simple and natural task, using markup tags. This class allows users to set values for variables, loops, if statements, etc. which are declared in the template.

eRuby is a templating system that embeds Ruby into a text document. It is often used to embed Ruby code in an HTML document, similar to ASP, JSP and PHP and other server-side scripting languages. The templating system of eRuby combines the ruby code and the plain text to provide flow control and variable substitution, thus making it easy to maintain.

Jinja (template engine) template engine for the Python programming language associated with the Flask framework

Jinja is a web template engine for the Python programming language. It was created by Armin Ronacher and is licensed under a BSD License. Jinja is similar to the Django template engine but provides Python-like expressions while ensuring that the templates are evaluated in a sandbox. It is a text-based template language and thus can be used to generate any markup as well as source code.

In computer programming, boilerplate code or just boilerplate are sections of code that have to be included in many places with little or no alteration. When using languages that are considered verbose, the programmer must write a lot of code to accomplish only minor functionality. Such code is called boilerplate.

Web template system System in web publishing that lets web designers and developers work with web templates to automatically generate custom web pages

A web template system in web publishing lets web designers and developers work with web templates to automatically generate custom web pages, such as the results from a search. This reuses static web page elements while defining dynamic elements based on web request parameters. Web templates support static content, providing basic structure and appearance. Developers can implement templates from content management systems, web application frameworks, and HTML editors.

Template processor Software designed to combine templates with a data model to produce result documents

A template processor is software designed to combine templates with a data model to produce result documents. The language that the templates are written in is known as a template language or templating language. For purposes of this article, a result document is any kind of formatted output, including documents, web pages, or source code, either in whole or in fragments. A template engine is ordinarily included as a part of a web template system or application framework, and may be used also as a preprocessor or filter.

Haml is a templating system that is designed to avoid writing inline code in a web document and make the HTML cleaner. Haml gives the flexibility to have some dynamic content in HTML. Similar to other web languages like PHP, ASP, JSP and template systems like eRuby, Haml also embeds some code that gets executed during runtime and generates HTML code in order to provide some dynamic content. In order to run Haml code, files need to have .haml extension. These files are similar to .erb or eRuby files which also help to embed Ruby code while developing a web application.

The PHP syntax and semantics are the format (syntax) and the related meanings (semantics) of the text and symbols in the PHP programming language. They form a set of rules that define how a PHP program can be written and interpreted.

CMS Made Simple

CMS Made Simple (CMSMS) is a free, open source (GPL) content management system (CMS) to provide developers, programmers and site owners a web-based development and administration area. In 2017 it won the CMS Critic annual award for Best Open Source Content Management.

PhpStorm PHP IDE

PhpStorm is a commercial, cross-platform IDE for PHP, built by the Czech Republic-based company JetBrains.

Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates. It's an open source product licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher. Symfony PHP framework comes with a bundled support for Twig as its default template engine since version 2.

Mustache is a web template system with implementations available for ActionScript, C++, Clojure, CoffeeScript, ColdFusion, Common Lisp, D, Dart, Delphi, Erlang, Fantom, Go, Haskell, Io, Java, JavaScript, Julia, Lua, .NET, Objective-C, OCaml, Perl, PHP, Pharo, Python, R, Racket, Ruby, Rust, Scala, Smalltalk, Swift, Tcl, CFEngine and XQuery.

ProcessWire is a free and open source content management system (CMS), content management framework (CMF) and web application framework (WAF) written in the PHP programming language. It is distributed under the Mozilla Public License 2.0. ProcessWire is built around an API with usage and naming conventions similar to the JavaScript framework jQuery. The stated goal behind the API is to provide the level of accessibility and control to pages in a website that jQuery provides to the DOM. Content is managed either via the API or the web-based admin control panel. ProcessWire is largely used for development of web sites, web applications, services, content feeds and related applications.

ASP.NET Web Forms is a web application framework and one of several programming models supported by the Microsoft ASP.NET technology. Web Forms applications can be written in any programming language which supports the Common Language Runtime, such as C# or Visual Basic. The main building blocks of Web Forms pages are server controls, which are reusable components responsible for rendering HTML markup and responding to events. A technique called view state is used to persist the state of server controls between normally stateless HTTP requests.

References

  1. https://github.com/smarty-php/smarty/releases/tag/v3.1.34
  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.