Skip to content
Derek Jones edited this page Jul 5, 2012 · 7 revisions

Category:Views

At this time, CodeIgniter (1.3.3) does not support fragment caching. To overcome this limitation, here is a technique that can be used in the event that you do need a fragment caching feature.

This technique involves breaking out a fragment into its own method, and from within the main view, referencing the URL of the fragment with a require() statement.

controllers/fragment.php

<?php

class Fragment Extends Controller {

function Fragment() {
parent::Controller();
}

function index() {
$data['current_time'] = date('U');
$this->load->view('time_view',$data);
}

function cached_time() {
$this->output->cache(10);
$data['cached_time'] = date('U');
$this->load->view('cached_time_view',$data);
}
}

?>

views/time_view.php

<p>The current time is: &lt;?=$current_time;?&gt;.</p>
&lt;?php require('http://localhost/index.php/fragment/cached_time');?&gt;

views/cached_time_view.php

<p>The cached time is: &lt;?=$cached_time;?&gt;.</p>
Clone this wiki locally