Smarty is a PHP template engine designed to separate the PHP code logic from the presentation layer of web applications. It creates more maintainable and readable code by clearly separating code and the web application template engine. Smarty is not a programming language or framework but rather a tool for rendering templates in a structured and efficient manner.
Some of the primary goals of Smarty include:
Separation of concerns: Smarty enforces a clear separation between the business logic (PHP code) and the presentation layer (HTML templates). This makes it easier for developers to maintain and update the codebase.
Improved code readability: Templates written in Smarty are more readable and maintainable than mixing PHP code with HTML. This separation helps designers and front-end developers work on the templates without needing in-depth knowledge of PHP.
Caching: Smarty provides built-in caching mechanisms, which can significantly improve web application performance by reducing the server and database load.
Customization: It allows the creation of custom functions and filters to extend the template engine’s functionality and meet specific project requirements.
Follow the steps below to use Smarty in PHP:
To get started, we need to install Smarty. We use a package manager like Composer to include it in our project.
composer init --no-interaction --name="vendor/autoload.php" --description="" --author="" --type="project" --license="MIT"composer require smarty/smarty
In the command above:
composer init
: Initiates the creation of a composer.json
file.
--no-interaction
: This flag prevents Composer from asking for user input during the initialization process.
--name="vendor/autoload.php"
: Sets the name of the package.
--description=""
: Sets the description of the package. In this example, it’s empty.
--author=""
: Sets the author’s information. It is left empty in the example but user can replace it with the project author’s name.
--type="project"
: Specifies the type of the package. In this case, it’s set to “project,” indicating that this is a project-level composer.json
file.
--license="MIT"
: Specifies the license for the project. In this example, it’s set to the MIT
license, which is an open-source license.
Create a project directory structure that separates the PHP code from the template files. For example:
- project_root/- templates/- header.tpl- footer.tpl- index.tpl- index.php- configs- templates_c
The purpose of each file is:
project_root/: Main project directory
templates/: Directory containing Smarty templates
header.tpl: Template file for the header section
footer.tpl: Template file for the footer section
index.tpl: Main template file for the page content
index.php: PHP file responsible for handling the application logic
configs/: Directory for storing configuration files
templates_c/: Directory where Smarty stores compiled templates for performance optimization
In the PHP code (e.g., index.php
), we need to initialize Smarty, define the templates
directory, and compile the directory. Here’s a basic initialization:
<?phprequire_once('vendor/autoload.php');$smarty = new Smarty();$smarty->setTemplateDir('templates/');$smarty->setCompileDir('templates_c/');
We can assign variables to our templates using the assign
method:
$smarty->assign('title', 'Smarty Project');$smarty->assign('heading', 'Hello, Smarty!');$smarty->assign('content', 'Welcome to Smarty!');
To render a template, use the display
method:
$smarty->display('index.tpl');
Create the template files inside the templates
directory. These templates will include Smarty tags ( e.g., {$dataname}
) for the dynamic data we assigned in our PHP code.
The example template named index.tpl
is provided below:
<html><head><title>{$title}</title></head><body><h1>{$heading}</h1><p>{$content}</p></body></html>
When we access our PHP script, Smarty will render the index.tpl
template with the assigned variables, and we’ll see the dynamically generated content on the web page.
The running example of Smarty in PHP is as follows:
<VirtualHost *:8080> ServerName {{EDUCATIVE_LIVE_VM_URL}} DocumentRoot /var/www/html/smarty/ <Directory /var/www/html/smarty> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/smarty_error.log CustomLog ${APACHE_LOG_DIR}/smarty_access.log combined </VirtualHost>
Smarty is a powerful and widely used template engine for PHP that helps maintain a clear separation between the logic and presentation layers of web applications. It enhances code readability, improves maintainability, and provides caching features. By following the steps outlined above, we can start using Smarty in our PHP projects and easily create dynamic, maintainable web applications.
Free Resources