Lightweight Template Engine with PHP

I can guide you through creating a lightweight template engine using PHP. A template engine is a tool that separates the presentation layer from the application logic, allowing developers to write HTML/CSS templates without mixing them with PHP logic directly. Here’s a basic implementation:

1. Requirements:

– Basic understanding of PHP.
– Text editor or IDE for code editing.

2. What is a Template Engine:

A template engine is a tool used to separate the presentation layer (HTML, CSS) from the application logic (PHP). It allows developers to create templates with placeholders that will be replaced with dynamic content at runtime.

3. Implementation:

We’ll create a simple template engine that replaces placeholders in template files with actual data.

<?php
// Template engine class
class TemplateEngine {
// Function to render a template with data
public static function render($template, $data = array()) {
// Extract data array into variables
extract($data);

// Include the template file
ob_start();
include $template;
return ob_get_clean();
}
}
?>

4.  Usage:

Let’s see how to use our template engine to render a template file with dynamic data.

<?php
// Include the template engine class
require_once 'TemplateEngine.php';

// Define some data
$title = 'Welcome to My Website';
$content = 'This is a sample content for the homepage.';

// Render the template with data
$output = TemplateEngine::render('template.php', ['title' => $title, 'content' => $content]);

// Output the rendered template
echo $output;
?>

5.  Creating Template File (template.php):

Create a template file with placeholders for dynamic data.

<!-- template.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<p><?php echo $content; ?></p>
</body>
</html>

6.  Explanation:

– The `TemplateEngine` class contains a `render` method that takes a template file path and an array of data.

– Inside the `render` method, the data array is extracted into variables using `extract`.

– The template file is included using `include`, and the output is captured using output buffering

(`ob_start` and `ob_get_clean`).

– Placeholders in the template file (e.g., `<?php echo $title; ?>`) are replaced with actual data values.

7.  Additional Features:-

You can extend the template engine to support more features like template inheritance, loops, conditionals, etc., depending on your requirements.

– Error handling and sanitization of data should also be considered to make the template engine more robust and secure.

This basic template engine provides a starting point for building more complex template systems in PHP. It’s lightweight and easy to use, making it suitable for small to medium-sized projects.

Add a Comment

Your email address will not be published. Required fields are marked *

ABOUT CODINGACE

My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib

CEO: codingace.com

Request a Quote









PHP Code Snippets Powered By : XYZScripts.com