📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials CodeIgniter 4 Views in CI4

Views in CI4

5 min read
Render views using CI4's template inheritance: extend(), section(), endSection(), and renderSection().

Views and Templates

// Return a view from controller
return view("posts/index", ["posts" => $posts]);

// Nested views
return view("layouts/header")
     . view("posts/index", ["posts" => $posts])
     . view("layouts/footer");

// View with layout (CI4 built-in)
// app/Views/layouts/main.php
<!DOCTYPE html>
<html>
<body>
    <?= $this->renderSection("content") ?>
</body>
</html>

// app/Views/posts/index.php
<?= $this->extend("layouts/main") ?>
<?= $this->section("content") ?>
<h1>Posts</h1>
<?php foreach ($posts as $post): ?>
    <h2><?= esc($post["title"]) ?></h2>
<?php endforeach ?>
<?= $this->endSection() ?>

// Helpers in views
esc($string);       // escape output (XSS safe)
site_url("/posts"); // absolute URL
base_url("images/logo.png");