Home Github
Slim Logo

Using Pesto with Slim

Integrate the Pesto template engine into your Slim projects to create expressive and modern views.

Integration Steps

1. Install Pesto & Slim

Start by installing Pesto and Slim via Composer.

composer require millancore/pesto php-di/php-di

2. Configure the Container

In your bootstrap file (e.g., bootstrap.php), configure the PHP-DI container to include the Pesto view factory.

use DI\Container;
use MillanCore\Pesto\PestoFactory;

$container = new Container();

$container->set('view', function () {
    return PestoFactory::create(
        __DIR__.'/resources/view',
        __DIR__.'/tmp/cache'
    );
});

return $container;

3. Create the Slim App

In your front controller (e.g., public/index.php), set up your Slim application.

use Slim\Factory\AppFactory;

$container = require dirname(__DIR__) . '/bootstrap.php';

AppFactory::setContainer($container);
$app = AppFactory::create();

4. Render a View in Your Route

Now you can use Pesto in your routes to render views.

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

$app->get('/', function (Request $request, Response $response, $args) {

    $homeView = $this->get('view')->make('home.php');

    $response->getBody()->write($homeView->toHtml());
    return $response;
});

5. Create a Pesto Template

Create your view file at resources/view/home.php. You can now use Pesto's syntax.

<h1>Hello, Pesto!</h1>

<ul>
    <li php-foreach="range(1, 5) as $i">
        Item {{ $i }}
    </li>
</ul>