In PHP, the preg_replace_callback_array function is a powerful tool that can process text based on regular expressions and callback functions. By cleverly utilizing this function, we can implement a simple template engine that easily embeds variables and logic into HTML templates.
This article will explain how to implement a simple template engine using preg_replace_callback_array , and we will demonstrate how to replace placeholders in templates using this function.
preg_replace_callback_array is a function in PHP that performs regular expression-based replacement operations and can specify a callback function for each matching regular expression. The callback function can be customized based on the matching content and returns the final replacement result.
The function prototype is as follows:
preg_replace_callback_array(array $patterns_and_callbacks, string $subject);
$patterns_and_callbacks : an array whose keys are regular expressions and values are callback functions.
$subject : The string to be processed.
We will create a simple template engine that replaces variables in templates and supports simple conditional logic. With preg_replace_callback_array , we can easily implement this function.
Suppose we have a simple HTML template, where the placeholders in the template are wrapped in {{}} :
$template = "<h1>welcome,{{name}}!</h1><p>{{message}}</p>";
$variables = [
'name' => 'Zhang San',
'message' => 'Here is a simple template engine example。',
];
We will use preg_replace_callback_array to define the placeholder replacement rules. We need to deal with two main tasks:
Replace variables (for example, {{name}} ) to actual values.
Handle custom logic, such as displaying different content according to conditions.
To implement these functions, we need to construct appropriate regular expressions and specify a callback function for each regular expression.
The following is the PHP code that implements the template engine:
<?php
$template = "<h1>welcome,{{name}}!</h1><p>{{message}}</p>";
$variables = [
'name' => 'Zhang San',
'message' => 'Here is a simple template engine example。',
];
// use preg_replace_callback_array Implement template engine
$patterns_and_callbacks = [
'/{{(.*?)}}/' => function ($matches) use ($variables) {
$key = $matches[1];
return isset($variables[$key]) ? $variables[$key] : '';
},
];
$output = preg_replace_callback_array($patterns_and_callbacks, $template);
echo $output;
The $patterns_and_callbacks array defines a regular expression /{{(.*?)}}/ , which matches placeholders in the template (e.g. {{name}} ).
For each matched placeholder, we replace it with the actual value in the $variables array via the callback function. If the corresponding variable cannot be found, the callback function returns an empty string.
In order to make the template engine stronger, we can add support for conditional statements, such as determining whether to display a certain content based on a certain condition. We can use syntax similar to {{if condition}} to implement this function.
Here is an example showing how to use simple conditional judgments in a template:
<?php
$template = "<h1>welcome,{{name}}!</h1>{{if show_message}}<p>{{message}}</p>{{endif}}";
$variables = [
'name' => 'Zhang San',
'message' => 'Here is a simple template engine example。',
'show_message' => true, // Control whether to display messages
];
// use preg_replace_callback_array Implement template engine
$patterns_and_callbacks = [
'/{{(.*?)}}/' => function ($matches) use ($variables) {
$key = $matches[1];
return isset($variables[$key]) ? $variables[$key] : '';
},
'/{{if (.*?)}}(.*?){{endif}}/s' => function ($matches) use ($variables) {
$condition = $matches[1];
$content = $matches[2];
// Analyze the conditions and judge
if (isset($variables[$condition]) && $variables[$condition]) {
return $content;
}
return '';
},
];
$output = preg_replace_callback_array($patterns_and_callbacks, $template);
echo $output;
A new regular expression /{{if (.*?)}}(.*?){{endif}}/s is added to match the conditional statement.
The callback function checks whether the condition in {{if condition}} is true. If true, {{message}} is displayed; if false, nothing is displayed.
Through preg_replace_callback_array , we can implement a very simple template engine that handles variable replacement and conditional judgment in templates. Although this engine is very basic, it shows how to use regular expressions and callback functions to implement the core functionality of the template engine. You can expand more complex functions on this basis, such as loops, filters, macro definitions, etc., and build a more powerful template engine.