View cells
Nhiều thành phần trong trang web xuất hiện lặp đi lặp lại ở các page khác nhau, ví dụ menu, form login, pop up … Codeigniter cho phép chúng ta đóng gói các thành phần như thế này thành các khối có thể tái sử dụng bằng cách sử dụng view cells. View cells là các mini-view có thể nhúng (include) ở trong view khác. View cells có thể xử lý logic riêng, nhờ đó tái sử dụng được và dễ hiểu trong việc phân tách logic xử lý riêng của từng phần cho class riêng.
Codeigniter hỗ trợ 2 loại view cells: simple và controlled. Simple view cells có thể tạo ra từ bất kỳ class hoặc method nào và không cần tuân theo điều kiện bất kỳ, ngoại trừ nó phải trả về 1 string. Controlled view cells thì phải tạo ra từ 1 class extend Codeigniter\View\Cells\Cell, cho phép tạo các view cells linh hoạt hơn, dùng nhanh hơn.
Gọi View Cells:
Trong view gọi view cells bằng hàm view_cell(). Tham số đầu tiên là tên class và method, tham số thứ 2 là 1 mảng các giá trị được truyền vào view. Method phải trả về 1 string.
<?= view_cell('MyClass::myMethod', ['param1' => 'value1', 'param2' => 'value2']) ?>Nếu bạn không include đầy đủ namespace của class, nó sẽ mặc định trong App\Cells namespace.
Bạn có thể truyền tham số bằng 1 chuỗi key/value như sau:
<?= view_cell('MyClass::myMethod', 'param1=value1, param2=value2') ?>Simple Cells:
Simple Cells là class trả về 1 string từ 1 method. Ví dụ 1 view cell thông báo tin nhắn:
namespace App\Cells;
class AlertMessage
{
public function show(array $params): string
{
return "<div class="alert alert-{$params['type']}">{$params['message']}</div>";
}
}Gọi view cell này trong view như sau:
<?= view_cell('AlertMessage::show', ['type' => 'success', 'message' => 'Successfull update!']) ?>Bạn cũng có thể sử dụng tên tham số khớp với biến trong method để dễ đọc. Ví dụ:
<?= view_cell('Blog::recentPosts', 'category=1, limit=5') ?>Ở trong Cell bạn viết như sau:
public function recentPosts(int $category, int $limit)
{
$posts = $this->blogModel
->where('category', $category)
->orderBy('published_on', 'DESC')
->limit($limit)
->get();
return view('recentPosts', ['posts' => $posts]);
}Controlled Cells
(Từ version 4.3.0)
Controlled cells có 2 mục đích: build cell nhanh hơn, và cung cấp logic nâng cao, linh hoạt hơn cho view. Class phải extend CodeIgniter\View\Cells\Cell. Phải có view file trong cùng thư mục. Tên class đặt dạng PascalCase (viết hoa chữ cái đầu các từ) + hậu tố Cell. File view đặt dạng snake_case, không cần hậu tố. Ví dụ file controlled cell là MyCell.php, class là MyCell, file view là my.php.
Tạo controlled cell:
Ví dụ tạo AlertMessage như trên bằng controlled cell:
Tạo file app/Cells/AlertMessageCell.php:
// app/Cells/AlertMessageCell.php
namespace App\Cells;
use CodeIgniter\View\Cells\Cell;
class AlertMessageCell extends Cell
{
public $type;
public $message;
}Trong class chỉ cần khai báo các biến public, nó sẽ tự động có sẵn trong file view.
Tạo file app/Cells/alert_message.php:
// app/Cells/alert_message.php
<div class="alert alert-<?= esc($type, 'attr') ?>">
<?= esc($message) ?>
</div>Trong view gọi hàm view_cell() để hiển thị như sau:
// Called in main View:
<?= view_cell('AlertMessageCell', 'type=warning, message=Failed.') ?>
Hi, this is a comment.
To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
Commenter avatars come from Gravatar.