What are View Cells in CodeIgniter 4?
Answer
View Cells in CI4 allow you to call a class method from within a view and insert its HTML output. They are reusable components that encapsulate both logic and a view. Define a cell class with a method that returns a view: namespace App\Cells; class RecentPosts { public function index(array $params = []): string { $posts = model("PostModel")->latest()->limit($params["count"] ?? 5)->findAll(); return view("cells/recent_posts", ["posts" => $posts]); } }. Call from a view: <?= view_cell("App\Cells\RecentPosts::index", ["count" => 3]) ?>. Cells can be cached: view_cell("App\Cells\RecentPosts::index", $params, 300) — cached for 300 seconds. View Cells are analogous to Laravel's View Components or Blade's @livewire — they encapsulate reusable view logic like sidebars, comment counts, and dynamic widgets.