Current Location: Home> Latest Articles> PHP Template Assignment: Custom assign() Function for Variable Injection and Extension

PHP Template Assignment: Custom assign() Function for Variable Injection and Extension

gitbox 2025-06-23

PHP Template Assignment: Custom assign() Function for Data Binding and Extension

In PHP development, to improve code maintainability and readability of views, template engines are commonly used to separate page structure from logic. To effectively pass data to templates, a custom assign() function can be implemented for template variable injection. This article explains how to create the assign() function and extend it to support additional data binding.

Basic Concept of Custom assign() Function

The main goal of the assign() function is to inject key-value pairs from a data array into a template file by performing simple string replacements.


function assign($data, $template) {
    // Assign array $data to template $template here
    // ...
}

This function takes two parameters: $data (the data array to inject) and $template (the template file path) to be used for replacement.

Implementation of assign() with Template Variable Replacement

The implementation reads the template content with file_get_contents() and uses str_replace() to substitute placeholders:


$templateContent = file_get_contents($template);
foreach ($data as $key => $value) {
    $placeholder = '{{' . $key . '}}';
    $templateContent = str_replace($placeholder, $value, $templateContent);
}
echo $templateContent;

This approach is suitable for lightweight template needs, especially when no complex template engine is introduced.

Using assign() for Template Data Binding

In practice, simply prepare the data array and template path, then call assign():


$data = [
    'name' => 'John Doe',
    'age' => 25,
];
$template = 'template.html';
assign($data, $template);

This replaces {{name}} and {{age}} placeholders in the template with corresponding values.

Supporting Additional Data: Adding the extra Parameter

To enhance flexibility, add a third parameter $extra to pass extra variables:


function assign($data, $template, $extra = []) {
    $templateContent = file_get_contents($template);
    foreach ($data as $key => $value) {
        $placeholder = '{{' . $key . '}}';
        $templateContent = str_replace($placeholder, $value, $templateContent);
    }
    foreach ($extra as $key => $value) {
        $placeholder = '{{' . $key . '}}';
        $templateContent = str_replace($placeholder, $value, $templateContent);
    }
    echo $templateContent;
}

This version allows injecting additional variables such as website or email into the template.

Example of Using assign() with extra Parameter

Here is an example using both main data and extra data:


$data = [
    'name' => 'John Doe',
    'age' => 25,
];
$template = 'template.html';
$extra = [
    'website' => 'example.com',
    'email' => '[email protected]',
];
assign($data, $template, $extra);

Placeholders like {{website}} and {{email}} will be replaced accordingly.

Summary

By creating a custom assign() function, you can flexibly assign array data to templates and extend it with additional parameters to meet diverse rendering needs. This method offers good template control without third-party engines and is suitable for small to medium projects or rapid prototyping.

Understanding the assign() function implementation and usage improves code clarity and modularity, making it an essential skill for efficient PHP development.