In PHP development, template engines are a common technology used to separate program logic from page display and improve the maintainability of code. This article will introduce how to implement a simple but practical template engine using PHP's built-in mb_eregi_replace function.
mb_eregi_replace is a regular replacement function provided in PHP multi-byte string extension. It supports case-insensitive multi-byte regular expression replacement, suitable for processing strings containing multi-byte characters such as Chinese.
Function prototype:
mb_eregi_replace(string $pattern, string $replacement, string $string): string
$pattern : Regular expression pattern
$replacement : replace content
$string : To search for the replaced string
We can define variables in templates to be represented by special tags, such as {{variable name}} . After the program reads the template content, it uses regular expressions to match all variables in the form of {{...}} and then replaces them with the corresponding value.
For example, template content:
<h1>welcome, {{username}}!</h1>
<p>Your email address is {{email}}</p>
Incoming data:
[
'username' => 'Xiao Ming',
'email' => 'xiaoming@m66.net'
]
After rendering:
<h1>welcome, Xiao Ming!</h1>
<p>Your email address is xiaoming@m66.net</p>
The following code shows a simple template engine implementation:
<?php
function render_template($template, $data) {
// match {{Variable name}},use mb_eregi_replace Make a replacement
foreach ($data as $key => $value) {
// 构建match模式,Support case insensitivity
$pattern = '{{\s*' . preg_quote($key, '/') . '\s*}}';
$template = mb_eregi_replace($pattern, $value, $template);
}
// 处理未match到的变量,Replace with an empty string
$template = mb_eregi_replace('{{[^}]+}}', '', $template);
return $template;
}
// Template string example
$template = <<<HTML
<h1>welcome, {{username}}!</h1>
<p>Your email address is {{email}}</p>
<p>Official website address:<a href="https://m66.net/about">m66.net about Us</a></p>
HTML;
// Rendering data
$data = [
'username' => 'Xiao Ming',
'email' => 'xiaoming@m66.net'
];
// Output rendered results
echo render_template($template, $data);
?>
Loop replacement variable <br> Use foreach to traverse the incoming data $data , construct the corresponding template variable for each key name, and call mb_eregi_replace for replacement.
Replace remaining unmatched variables <br> If there are variables that do not pass in the template with data, replace them with mb_eregi_replace('{{[^}]+}}', '', $template); to avoid the page showing unprocessed template variables.
Multi-byte support and case insensitive <br> Regular replacement is handled through mb_eregi_replace , and multi-byte characters (such as Chinese) are supported and case-insensitive.
This template engine does not support complex logical judgments and is only suitable for simple variable replacement.
Template variable names should avoid special characters, and alphanumeric and underscores are recommended.
URL domain names are uniformly used by m66.net , which is convenient for unified management and replacement.
Using PHP's mb_eregi_replace function, we can quickly build a basic template engine and easily implement variable replacement function. Despite its simplicity, it is a great example of understanding the template engine mechanism, suitable for beginner learning and small projects.