Plates PHP, system of templates / templates

Native templates in PHP with Plates. Description, features and first steps with Plates, the template system for PHP.

The templates or in English templates is one of the typical components of a web application. In PHP there are various template libraries and many are very popular like Smarty. This time we see a template system that is not so popular but is quite powerful, simple and fast.

When we work with template systems, sometimes we have to learn new syntaxes to perform the typical operations with templates, injection of data into them, iteration over structures such as arrays, etc. The syntax of the template system is usually different from that of the language itself where we are working and although this is generally not a very serious problem (because this syntax is always simple and easy to memorize) it forces the templates to be compiled before being used.

In the case of “Plates” we have a native PHP template system. That is, a system that does not implement new syntax, but rather uses PHP’s own syntax and control structures to be able to express typical operations. Therefore, it also does not require compilation of the templates like Twig or Smarty.

Its name comes from temPLATES and we can find it at this address:

It is an ideal solution for all those who prefer to express templates with PHP, but without giving up advanced template system features. It is inspired by Twig, the symfony templating engine. It is fast and helps you solve various needs without too many complications, read:

  • template inheritance
  • Creation of multiple layouts
  • Organization of templates in namespaced directories
  • Sending data to the templates and the possibility of sharing them among several
  • Extensibility by functions or groups of functions (extensions)
  • Escape functions for data dumped into templates
  • Independent of the framework or application, adaptable to any project.
See also  Introduction to GitLab

Installing PlatePHP

The recommended way to install this template library is through Composer, where it will be as simple as indicating the dependency. You can find it on Packagist at this URL:

You just have to add the require in your composer.json. (“league/plates”: “3.*”)

The important thing that you should have learned in that manual is how to activate composer’s autoload system in PHP code, simply by making a require of the file located in vendor/autoload.php.

require “../vendor/autoload.php”;

Once the Composer autoload system is activated, we will be able to access the installed dependencies from any part of the code, as long as we use their namespace correctly.

Using the Plates template system

Once the dependency is installed (and Composer’s autoload is invoked), to start using your templates you must instantiate the Engine class, on which the entire template engine depends.

$templates = new League\Plates\Engine( ‘path/to/the/templates’);

As you can see, when you instantiate the Engine you must indicate the path where your templates are.

Note: The template path can be anywhere in the server’s file system. The Plates documentation doesn’t report very well on whether the path is relative or absolute. I have tried both and it works. It seems more logical to me to use relative paths whenever possible, but it is also useful to indicate the absolute path to the directory where you have the templates, for example: /var/www/example/application/views.

If you have all the templates in the same directory, or subdirectories of it, you already have it ready. But if you prefer to organize them by different directories in dispersed paths, you only have to add them in the Engine.

See also  How to truncate a table with foreign key: MySQL Cannot truncate error

$templates->addFolder(‘template_namespace’, ‘other/different/path’);

To execute a template we have to indicate which template we want to render. There are several ways, but the fastest is to simply use the render() method of the Engine, indicating the name of the template and the data that we want to send to it.

$templates->render(“template_to_render”, );

The template that is expected “template_to_render” has a “.php” extension, but we could modify it if desired, but as you can see, it is not necessary to indicate “.php” in the name of the template to invoke. If the template is in a subdirectory you can indicate it with a slash.

$templates->render(“subfolder/template_to_render”);

As you can see, the data that is sent to the template is indicated only optionally. That is, if we don’t have to send you data, we don’t need to tell you anything. The data that reaches the template script will be found in the global scope, that is, as if they were global variables.

If the template is in another folder than the ones you saved with addFolder(), you indicate the namespace of that path with “::” separating the name of the template you’re looking for.

$templates->render(‘template_name_space::template_to_render’);

Using Layouts

Generally, every website has a header and a footer. With layouts you can get the entire structure of the page (both the header and the footer) to be in the same file.

Here you can see a layout: (milayout.php file)

company title

Company logo company

section(‘content’)?>

Footer Links

As you can see, the $this->section(‘content’) block is what will be placed as the body of the page for this layout.

See also  what is spyware

Now you can see a template that uses this layout.

layout(‘milayout’) ?>

This is the body for this page.

Like PlatesPHP

With the first line $this->layout(‘milayout’) we are indicating that the HTML of the layout to be used is in milayout.php. The rest of the document is the body that will be placed in the template, once the layout has been applied.

conclusion

Before we finish we want to provide you with a link to a GitHub repository where we have posted sample code that works with the PHP Plates template system and uses a layout. It’s a very simple project, but it can give you a more global idea about how to organize your templates. you find it in

With this article we have told you very much above what can be done with Plates and we have given you one of the many alternatives that exist to produce HTML based on templates. But this article should not confuse you, if you want to go deeper into Plates you will see that the template system can be made much more complex. It’s simple for those just starting out with PHP templating systems, but advanced enough for those who want to take advantage of advanced features typical of modern templating engines.

The plates site is well documented and easy to read and learn on the fly what you need. The only thing missing is a little more community and third-party tutorials to clarify possible doubts.

Loading Facebook Comments ...
Loading Disqus Comments ...