CI4

[코드이그나이터] 뷰 셀(View Cell)

으누아빠 2020. 9. 4. 19:20
반응형

뷰 셀(View Cell)

출처:
http://ci4doc.cikorea.net/outgoing/view_cells.html

컨트롤러 외부에서 생성된 HTML을 삽입할 수 있음

CI3 의 Widget 기능을 view_cell 이 대응하는것 같음

modules\main\Views\MainView.php

<html>

<head>
    <title>Main 뷰</title>
</head>

<body>
    <h1>메인 뷰 입니다.</h1>
    <?= view_cell('\App\Libraries\Widget::recentWidget', 'category=codeigniter, limit=5') ?>
</body>

</html>

가독성을 높이기 위해 메소드의 매개 변수와 일치하는 매개 변수 이름을 사용

app\Libraries\Widget.php

<?php

namespace App\Libraries;

class Widget
{
    public function recentWidget(int $limit, string $category)
    {
        echo $category;
        echo $limit;

        $data = array();
        $data['category'] = $category;
        $data['limit'] = $limit;

        return view('widgets/recentWidget', $data);
    }
}
app\Views\widgets\recentWidget.php

<h4>여기는 위젯 영역입니다.</h4>
<p> <?= $limit; ?> </p>
<p> <?= $category; ?> </p>

셀 캐싱 (Caching)

view_cell() 의 세 번째 파라미터 로 캐싱 시간(초)을 전달하여 뷰 셀 호출 결과를 캐시(cache)할 수 있음

 // Cache the view for 5 minutes
<?= view_cell('\App\Libraries\Blog::recentPosts', 'limit=5', 300) ?>