Integrate the Pesto template engine into your CodeIgniter projects to create expressive and modern views.
Start by installing Pesto via Composer.
composer require millancore/pesto
Create a Codeigniter Pesto View file at app/Libraries/PestoAdapter.php. This will handle the 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);
}
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']);
}
}
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>