DEV Community

Cover image for Plates Native PHP Template
Antonio Silva
Antonio Silva

Posted on

Plates Native PHP Template

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
Enter fullscreen mode Exit fullscreen mode
{
    "require": {
        "league/plates": "3.*"
    }
}
Enter fullscreen mode Exit fullscreen mode

Be sure to also include your Composer autoload file in your project:

require 'vendor/autoload.php';
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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') ?>
Enter fullscreen mode Exit fullscreen mode

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']) ?>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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]);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)