Use Template | PHP 8.0 "Way to PHP (Chinese version of PHP The Right Way)

Posted by the_crazyman on Thu, 18 Nov 2021 18:27:43 +0100

Templates provide a convenient way to separate controller and domain logic from presentation logic. Templates usually contain HTML for applications, but they can also be used in other formats, such as XML. Templates, often referred to as "views", make up Model-View-Controller (MVC) Part of the second component of the software architecture pattern.

benefit

The main benefit of using templates is that they create a clear separation between the presentation logic and the rest of the application. Templates are only responsible for displaying formatted content. They are not responsible for data discovery, persistence, or other more complex tasks. This results in clearer, more readable code, which is particularly helpful for developers working with server-side code (controllers, models) and front-end people working with front-end code.

Templates can also improve code structure. Templates are usually placed in the "views" folder, and each template is defined in a file. This approach encourages code reuse by splitting large blocks of code into smaller, reusable parts, often called partitions. For example, you can define a site header and footer as templates, and then include templates before and after each page template.

Finally, depending on the library you use, templates can provide more security by automatically escaping user-generated content. Some libraries even provide sandboxes, where template designers can only access variables and functions from the whitelist.

Common PHP Template

Common PHP templates are simply templates that use native PHP code. They are a natural choice because PHP is actually a template language itself. This means that you can combine PHP code, such as HTML, in other code. This is beneficial for PHP developers because there is no new syntax to learn, they know the functions available, and their code editor already has PHP syntax highlighting and autocompletion built-in. Additionally, common PHP templates tend to be very fast because there is no compilation phase required.

Modern PHP frameworks use a template system, most of which use native PHP syntax. Outside the frame, like Plates or Aura.View These libraries provide modern template functionality such as inheritance, layout, and extension, making native PHP templates easier to use.

Simple example of native PHP template

Use Plates class libraries

<?php // user_profile.php ?>

<?php $this->insert('header', ['title' => 'User Profile']) ?>

<h1>User Profile</h1>
<p>Hello, <?=$this->escape($name)?></p>

<?php $this->insert('footer') ?>

Example of native PHP template using inheritance

Use Plates class libraries

<?php // template.php ?>

<html>
<head>
    <title><?=$title?></title>
</head>
<body>

<main>
    <?=$this->section('content')?>
</main>

</body>
</html>
<?php // user_profile.php ?>

<?php $this->layout('template', ['title' => 'User Profile']) ?>

<h1>User Profile</h1>
<p>Hello, <?=$this->escape($name)?></p>

Compile Template

Although PHP has evolved into a mature object-oriented language, it serves as a template language Not much improvement . Compile templates, such as TwigBrainy , or Smarty [Note] A new grammar specific to templates is provided to fill this gap. From automatic escalation to inheritance and simplified control structures, compilation templates are designed to be easier to write, clearer to read, and safer to use. Compiled templates can even be shared across different languages. Mustache This is a good example. Since these templates must be compiled, they have a slight impact on performance, but a very small impact when caching is used appropriately.

[Note] Although Smarty provides automatic escape, it is off by default

Simple example of compiling a template

Use Twig Class library.

{% include 'header.html' with {'title': 'User Profile'} %}

<h1>User Profile</h1>
<p>Hello, {{ name }}</p>

{% include 'footer.html' %}

Example compilation template using inheritance

Use Twig Class library.

// template.html

<html>
<head>
    <title>{% block title %}{% endblock %}</title>
</head>
<body>

<main>
    {% block content %}{% endblock %}
</main>

</body>
</html>
// user_profile.html

{% extends "template.html" %}

{% block title %}User Profile{% endblock %}
{% block content %}
    <h1>User Profile</h1>
    <p>Hello, {{ name }}</p>
{% endblock %}

Extended reading

Articles and Tutorials

class libraries

This article begins with LearnKu.com On the website.