Current Location: Home> Latest Articles> How to Implement Page Separation with Template Engines in PHP: A Detailed Guide Using Smarty

How to Implement Page Separation with Template Engines in PHP: A Detailed Guide Using Smarty

gitbox 2025-06-16

1. What is a Template Engine?

A template engine is a technology that separates data from presentation. The core idea is to decouple business logic from presentation logic. It achieves this by separating page markup and presentation logic (such as for loops and if statements), thus improving page generation efficiency, reducing redundant code, and making the system easier to maintain.

2. Why Use a Template Engine?

In traditional development, business logic and presentation logic are often mixed together, making code maintenance complex. By using a template engine, these two can be separated, making the code more modular, easier to maintain, reuse, and reducing repetitive tasks, ultimately improving development efficiency.

3. How to Implement Page Separation with a Template Engine

In PHP, you can use third-party template engines such as Smarty or Twig to achieve page separation. In this article, we will use Smarty as an example to explain how to implement page separation with a template engine.

3.1 Installing Smarty

First, download Smarty and extract it to the PHP include_path directory. Suppose we have extracted Smarty to the /vendor/smarty/ directory, then we need to include the Smarty class library in the PHP file. The sample code is as follows:


require_once('/vendor/smarty/Smarty.class.php');

3.2 Creating a Smarty Instance

Next, we need to create an instance of the Smarty class. The code is as follows:


$smarty = new Smarty();
$smarty->setTemplateDir(__DIR__ . '/templates/');
$smarty->setCompileDir(__DIR__ . '/templates_c/');
$smarty->setCacheDir(__DIR__ . '/cache/');
$smarty->setConfigDir(__DIR__ . '/configs/');
$smarty->left_delimiter = '{';
$smarty->right_delimiter = '}';

In this code, we first create an instance of Smarty and then set the template directory, compile directory, cache directory, and config directory. These directories must be writable. Additionally, we set the left and right delimiters for Smarty templates to avoid conflicts with PHP syntax.

3.3 Rendering Templates

Next, we need to create a template file (e.g., index.tpl), as shown in the example below:


{ $title }
Welcome to { $title }
Today is { $date }

In this template file, we use Smarty's variable replacement syntax to insert the $title and $date variables into the template at the appropriate locations.

Then, in the PHP file, we use Smarty's fetch method to render the template. The sample code is as follows:


$smarty->assign('title', 'My Website');
$smarty->assign('date', date('Y-m-d'));
$smarty->display('index.tpl');

In this code, we assign values to the $title and $date variables, then call Smarty's display method to render the template. The result displayed in the browser will be as follows:


<title>My Website</title>
<h1>Welcome to My Website</h1>
<p>Today is 2022-01-01</p>

4. Conclusion

Using a template engine allows you to separate business logic from presentation logic, reducing maintenance and modification costs. In this article, we used Smarty as an example to demonstrate how to implement page separation in PHP. By mastering the installation of Smarty, instance creation, template rendering, and other basic operations, developers can build more efficient and maintainable PHP projects.