PHP has a new Template Engine.

Juan MillanJuan Millan
2 min read

Pesto is a traditional Italian sauce that originated in Genoa (Liguria region), It is also the place where a new template engine for PHP is being developed, seeking simplicity, minimal code, and full compatibility with HTML.

Pesto is a project with zero dependencies and in constant evolution, yuo can check it directly on Github.

composer require millancore/pesto

The first time you see the syntax, you know what's going to happen. It's expressive.

XSS Security

Pesto only uses one type to print PHP expressions {{ $variable }}, And all of them are escaped according to the context in which they are found, whether they are in a js block, css, html attribute or comment, it is able to determine what their context is and how it should escape the values to make them secure.

It is also possible to specify that we do not want to escape, thanks to filters. {{ $trust | raw }}

php-* Attributes

Very few attributes are actually used, but they are more than enough to build interfaces.

  • php-partial To incluide other views, this is use with php-with to pass data to partial view.

  • php-slot It is used to define the content of a slot (only named slots) defined in a partial view.

  • php-if php-elseif php-else simple condition

  •   <p php-if="$user->isAdmin()">Admin</p>
      <p php-elseif="$user->isModerator()">Moderator</p>
      <p php-else>Guest</p>
    

    php-foreach Iterate elements

  •   <li php-foreach="$list as $item">{{ $item }}</li>
    

The <template> Tag

We do not always want to include the HTML tag where we have defined the attribute. We can use the <template> tag, which allows us to organise the view in a better way, The template tag is not included in the final view.

<template php-foreach="$list as $item">
    <li>{{ $item }}</li>
</template>

View Composition

In Pesto, any view can be used as a partial, as many times as necessary. Simply define the slots that will have the content defined in the final view, Although the “| slot” filter is not mandatory, but provides a visual marker that defines it as a slot.

<!--- layouts/app.php -->
<!DOCTYPE html>
<html lang="en">
<head>
    <title>{{ $title }}</title>
</head>
<body>
    <header>{{ $header | slot }}</header>

    <main>{{ $main | slot }}</main>
</body>
</html>

View:

<!--- views/home.php -->
<template php-partial="layouts/app.php" php-with="['title' => 'Home']">

    <!-- Named slot -->
    <nav php-slot="header">
        <a href="/">Home</a>
        <a href="/about">About</a>
    </nav>

    <!--Main Slot -->
    <section>
        <h1>Home</h1>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quae.</p>
    <section>
</template>

You can find installation instructions and more detailed usage notes in the repository.

https://github.com/millancore/pesto

Thanks

I will write several articles about pesto. I hope you enjoy them as much as I enjoyed building it.

0
Subscribe to my newsletter

Read articles from Juan Millan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Juan Millan
Juan Millan