Upgrade Views
Documentations
What has been changed
Your views look much like before, but they are invoked differently … instead of CI3’s
$this->load->view(x);
, you can usereturn view(x);
.CI4 supports View Cells to build your response in pieces, and View Layouts for page layout.
The template parser is still there, and substantially enhanced.
Upgrade Guide
First, move all views to the folder app/Views
- Change the loading syntax of views in every script where you load views:
from
$this->load->view('directory_name/file_name')
toreturn view('directory_name/file_name');
from
$content = $this->load->view('file', $data, TRUE);
to$content = view('file', $data);
(optional) You can change the echo syntax in views from
<?php echo $title; ?>
to<?= $title ?>
Code Example
CodeIgniter Version 3.x
Path: application/views:
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $heading; ?></h1>
<h3>My Todo List</h3>
<ul>
<?php foreach ($todo_list as $item): ?>
<li><?php echo $item; ?></li>
<?php endforeach; ?>
</ul>
</body>
</html>
CodeIgniter Version 4.x
Path: app/Views:
<html>
<head>
<title><?= esc($title) ?></title>
</head>
<body>
<h1><?= esc($heading) ?></h1>
<h3>My Todo List</h3>
<ul>
<?php foreach ($todo_list as $item): ?>
<li><?= esc($item) ?></li>
<?php endforeach ?>
</ul>
</body>
</html>