Home > Backend Development > PHP Tutorial > Template engine for PHP functions

Template engine for PHP functions

PHPz
Release: 2023-05-18 18:56:01
Original
1626 people have browsed it

Template engine of PHP function

In Web development, the template engine is an essential part. It can mix dynamic data and template files to generate final HTML code. PHP language is one of the commonly used development languages ​​in web development, and naturally there are many excellent template engines. Among them, the template engine implemented using PHP functions is not only simple and easy to use, but also does not require additional libraries or extensions.

This article will introduce how to use PHP functions to implement a simple template engine.

1. Template engine implementation principle

The implementation principle of the template engine is very simple: use placeholders to replace dynamic data in the template file, and replace the placeholders with actual data in the code .

For example, a template file template.html containing placeholders:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{$title}</title>
</head>
<body>
    <h1>{$title}</h1>
    <p>{$content}</p>
</body>
</html>
Copy after login

where, {$title} and { $content} is a placeholder, representing the dynamic data that needs to be replaced.

In the PHP code, the code for reading the template file and parsing the placeholders is as follows:

$data = array(
    'title' => '标题',
    'content' => '内容'
);

$template = file_get_contents('template.html');

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

echo $template;
Copy after login

This code first reads the contents of the template file and replaces the placeholders with actual data Replace and eventually output the generated HTML code.

2. Use PHP functions to implement the template engine

In order to facilitate reuse and integration, we can encapsulate the implementation of the template engine into a function. The following is a simple implementation:

function render_template($template_file, $data) {
    $template = file_get_contents($template_file);
    
    foreach ($data as $key => $value) {
        $template = str_replace('{$'.$key.'}', $value, $template);
    }
    
    return $template;
}
Copy after login

This function accepts two parameters, $template_file represents the path to the template file, and $data represents the dynamic data that needs to be replaced. The return value of the function is the generated HTML code.

Sample code for calling this function:

$data = array(
    'title' => '标题',
    'content' => '内容'
);

$html = render_template('template.html', $data);

echo $html;
Copy after login

3. Use template inheritance to implement complex templates

For complex templates, you may need to use template inheritance to implement them. Template inheritance is a template reuse mechanism that can merge multiple template files into a complete template file.

The following is an example implemented using template inheritance: Suppose we have two template files, base.html and page.html, where base. html is a template file that contains the entire page frame, and page.html is a template file that contains the page content.

base.html The content of the file is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{$title}</title>
</head>
<body>
    <div id="header">
        <h1>{$site_name}</h1>
    </div>
    
    <div id="content">
        {% block content %}{% endblock %}
    </div>
    
    <div id="footer">
        © 2020 {$site_name}
    </div>
</body>
</html>
Copy after login

Among them, we use the title, website name, etc. in the page as placeholders, and use part of the page content {% block content %}{% endblock %} Including it means that this part of the content will be used in the child template.

page.html The content of the file is as follows:

{% extends "base.html" %}

{% block content %}
    <h2>{$title}</h2>
    <p>{$content}</p>
{% endblock %}
Copy after login

Among them, the use# is specified by {% extends "base.html" %} ##base.html serves as the parent template and contains the template of the page content part through {% block content %} and {% endblock %}, so that it can be used in the parent This part of the content is controlled and customized in the template.

The sample code for the final generated page is as follows:

$data = array(
    'title' => '页面标题',
    'content' => '页面内容',
    'site_name' => 'My Site'
);

$html = render_template('page.html', $data);

echo $html;
Copy after login
During the rendering process, the

page.html template file will be loaded first, and it is found that it uses base.html is used as the parent template, so continue to load the base.html template file. When loading the parent template file, since {% block content %} and {% endblock %} are used to define the page content part, the ## in the child template can be The content in #{% block content %} and {% endblock %} is replaced with the part of {% block content %}{% endblock %} in the parent template. 4. Conclusion

This article introduces how to use PHP functions to implement a simple template engine and implement complex templates by using template inheritance. The template engine implemented by PHP functions is simple and easy to use, suitable for small projects or rapid prototype development. If you need more advanced functions, such as caching, compilation, debugging, etc., it is recommended to use existing open source template engine libraries, such as Smarty, Twig, etc.

The above is the detailed content of Template engine for PHP functions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template