Home Github
CodeIgniter Logo

Using Pesto with CodeIgniter

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

Integration Steps

1. Install Pesto

Start by installing Pesto via Composer.

composer require millancore/pesto

2. Create a Pesto Adapter

Create a Codeigniter Pesto View file at app/Libraries/PestoAdapter.php. This will handle the renderer service.

3. Register PestoAdapter as renderer Service

Open app/Config/Service.php and add service.

   public static function renderer(?string $viewPath = null, ?ViewConfig $config = null, bool $getShared = true)
    {
        if ($getShared) {
            return static::getSharedInstance('renderer', $viewPath, $config);
        }

        $viewPath = $viewPath !== null && $viewPath !== '' && $viewPath !== '0' ? $viewPath : (new Paths())->viewDirectory;
        $config ??= config(ViewConfig::class);

        return new PestoAdapter($config, $viewPath, CI_DEBUG);
    }

4. Render a View in Your Controller

Now you can use the view() function in your controllers to render Pesto templates.

namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        return view('welcome_message', ['title' => 'Welcome to CodeIgniter! with Pesto']);
    }
}

5. Create a Pesto Template

Create your view file at app/Views/welcome_message.php. You can now use Pesto's syntax.

<h1>{{ $title }}</h1>

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