PHPackages                             millancore/pesto - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. millancore/pesto

ActiveLibrary[Templating &amp; Views](/categories/templating)

millancore/pesto
================

PHP View Engine

v0.2.0(1mo ago)381302[1 PRs](https://github.com/millancore/pesto/pulls)MITPHPPHP ^8.4CI passing

Since Aug 22Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/millancore/pesto)[ Packagist](https://packagist.org/packages/millancore/pesto)[ GitHub Sponsors](https://github.com/millancore)[ RSS](/packages/millancore-pesto/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (2)Dependencies (20)Versions (9)Used By (0)

Pesto
=====

[](#pesto)

Modern PHP template engine that provides an intuitive and expressive way to build web application views. It offers a clean syntax using custom HTML attributes and supports advanced templating features like view composition, slots, conditional rendering, loops, and built-in security measures.

Pesto understands the context of `{{ variables }}`and escapes them according to their scope to avoid [Cross-Site Scripting](https://en.wikipedia.org/wiki/Cross-site_scripting) (XSS) issues.

```

    Item {{ $number }}

```

Or, for greater clarity, use ``, which will not be included in the final render.

```

       Item {{ $number }}

```

Pesto templates support files with the `.html` or `.php` extension, allowing you to integrate PHP code if needed.

- [Installation &amp; usage](#installation--usage)
- [View Composition](#view-composition)
    - [The `` Tag](#template-tag)
    - [Partials](#partials--slots)
    - [Slots](#partials--slots)
    - [Nested Views](#nested-views)
- [Control Flow](#control-flow)
    - [If Attribute](#if-attribute)
    - [Loop](#loops)
    - [Inline](#inline)
- [Short Syntax](#short-syntax)
- [Command Line](#command-line)
    - [Compile](#compile)
    - [Lint](#lint)
- [Filters](#filters)
    - [Chain Filters](#chain-filters)
    - [Filters with Arguments](#filters-with-arguments)
    - [Add Filters](#add-filters)
- [Benchmarks](#benchmarks)

Installation &amp; Usage
------------------------

[](#installation--usage)

- PHP ^8.4

Pesto is available via Composer and is free of third-party dependencies

```
composer require millancore/pesto
```

```
use MillanCore\Pesto\PestoFactory;

$pesto = PestoFactory::create([
    templatesPath: __DIR__ . '/views',
    cachePath: __DIR__ . '/cache',
    // [ New CustomFilters(), ... ]
]);

$pesto->make('view.php', ['user' => $user]);
```

View Composition
----------------

[](#view-composition)

Pesto makes it easy to reuse parts of your views

### Template Tag

[](#template-tag)

The `` tag allows you to define php-\* attributes that will be evaluated but not the tag included in the final render.

- `Admin` --&gt; `Admin`
- `Admin` --&gt; `Admin`

### Partials &amp; Slots

[](#partials--slots)

When working with views that are composed of other views, you can use partials and slots to avoid repetition.

Layout

```

>

    {{ $title }}

    {{ $header | slot }}

    {{ $main | slot }}

```

View:

```

        Home
        About

        Home
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quae.

    Item

            nested item
            ....

```

Control Flow
------------

[](#control-flow)

Pesto provides two control flow directives: `foreach` and `if`, enough to build any kind of view.

### If Attribute

[](#if-attribute)

The only rule importance for use `php-elseif` and `php-else` is the tag must be a sibling of the `php-if` tag.

- `php-if`
- `php-elseif`
- `php-else`

`php-if` allows you to conditionally render a block of code.

```
Admin
Moderator
Guest
```

### Loops

[](#loops)

Pesto provides a simple way to loop over arrays or objects.

```
{{ $item }}
```

### Inline

[](#inline)

Pesto also allows you to use inline control flow directives.

```

      {{ $user->name | title }} - {{ $user->email }}

```

Short Syntax
------------

[](#short-syntax)

Every `php-*` attribute has a shorter `p:*` alias, both forms work everywhere and can be mixed in the same template.

Long formShort form`php-if``p:if``php-elseif``p:elseif``php-else``p:else``php-foreach``p:foreach``php-partial``p:partial``php-with``p:with``php-slot``p:slot````

    {{ $item }}

No items
```

If an element has both forms of the same directive, the long form wins. Since no client-side framework claims the `p:` prefix, it is safe to combine with Vue, Alpine.js, or Lit bindings.

Command Line
------------

[](#command-line)

Pesto ships with a `pesto` binary (installed at `vendor/bin/pesto`) to validate and inspect templates without rendering them.

```
vendor/bin/pesto help
```

CommandDescription`pesto compile `Validate and compile a template, print the result`pesto -c `Shorthand for `compile``pesto lint  [...]`Validate template files or directories`pesto help`Show the help messageBoth commands read the template from stdin when no path is given (or with `-`).

### Compile

[](#compile)

Compiles a template and prints the resulting PHP, so you can see exactly what Pesto generates:

```
echo '{{ $item->name | title }}' | vendor/bin/pesto compile
```

```
