Description #
Smarty is a template engine for PHP that separates presentation (HTML/CSS) from application logic. It uses its own clean syntax to embed variables, loops, and conditionals in template files.
History #
Smarty was created by Monte Ohrt and first released in 2001 as a project of the PHP development company New Digital Group. It became one of the earliest and most popular PHP templating systems.
Hello World Code #
<h1>{$message}</h1>
With backend PHP:
$smarty->assign('message', 'Hello, World!');
$smarty->display('hello.tpl');
How to Run #
Option 1: Online
Try via hosted demo (limited availability) or test PHP+Smarty locally.
Option 2: Local
Install via Composer:
composer require smarty/smarty
Basic setup:
require_once 'vendor/autoload.php';
$smarty = new Smarty();
$smarty->assign('message', 'Hello, World!');
$smarty->display('hello.tpl');
Key Concepts #
- Uses
.tpl
template files - Variables with
{$variable}
syntax {foreach}
,{if}
,{include}
for logic and layout- Clean separation of logic and design
- Supports modifiers, custom functions, and plugins
- Caches compiled templates for performance
- Built-in security filters
- Widely used in legacy PHP apps and CMSs
- Integrates easily with MVC PHP frameworks
- Open-source under the LGPL
Try It Online #
Most easily tested via local PHP setup
Smarty examples and demos: https://www.smarty.net/
Fun Facts #
- One of the first PHP template engines to gain mainstream adoption.
- Still used in legacy systems and platforms like Prestashop and CMS Made Simple.
- Known for its balance of power, safety, and ease of use.
Resources #
Official site
Docs or tutorial
GitHub or interpreter
Community or learning resources