Official website of the local reference plates
About
Plates is a native PHP template system that’s fast, easy to use and easy to extend. It’s inspired by the excellent Twig template engine and strives to bring modern template language functionality to native PHP templates. Plates is designed for developers who prefer to use native PHP templates over compiled template languages, such as Twig or Smarty.
Installation
Plates is available on Packagist and can be installed using Composer. This can be done by running the following command or by updating your composer.json file.
composer require league/plates
{
"require": {
"league/plates": "3.*"
}
}
Be sure to also include your Composer autoload file in your project:
require 'vendor/autoload.php';
Simple Example
Here is a simple example of how to use Plates. We will assume the following directory stucture:
📦Plates
┣ 📂templates
┃ ┣ 📜profile.php
┃ ┗ 📜template.php
┗ 📜index.php
Define a layout
The layout() function allows you to define a layout template that a template will implement. It’s like having separate header and footer templates in one file.
<?php $this->layout('template') ?>
The layout() function can be called anywhere in a template, since the layout template is actually rendered second. Typically it’s placed at the top of the file.
Assign data
To assign data (variables) to a layout template, pass them as an array to the layout() function. This data will then be available as locally scoped variables within the layout template.
<?php $this->layout('template', ['title' => 'Simple example']) ?>
The page template
<?php $this->layout('template', ['title' => 'Simple example']) ?>
<h1>Fruit List</h1>
<ul>
<?php foreach( $fruits as $fruit): ?>
<li>
<?=$this->e($fruit)?>
</li>
<?php endforeach ?>
</ul>
Accessing the content
To access the rendered template content within the layout, use the section() function, passing 'content' as the section name. This will return all outputted content from the template that hasn’t been defined in a section.
The layout template
<html>
<head>
<title><?=$this->e($title) ?></title>
</head>
<body>
<?=$this->section('content')?>
</body>
</html>
Escaping
Escaping is a form of data filtering which sanitizes unsafe, user supplied input prior to outputting it as HTML
<h1>Hello, <?=$this->escape($name)?></h1>
<!-- Using the alternative, shorthand function -->
<h1>Hello, <?=$this->e($name)?></h1>
Within your controller
<?php
require 'vendor/autoload.php';
// Create new Plates instance
$templates = new League\Plates\Engine('templates');
$fruits = ['Apple','Banana', 'Watermelon', 'Orange', 'Grape'];
// Render a template
echo $templates->render('profile', ['fruits' => $fruits]);