Welcome Guest, Not a member yet? Register   Sign In
How to cache a modified view?
#1

[eluser]gminuses[/eluser]
I try to compress my view and then cache it like this:
Code:
class MyController extends Controller {
    function myMethod() {

        $html = $this->load->view('myview', '', true);

        compressHtml($html);

        $this->output->cache(1);

        echo $html;
    }
}
But the user guide said
Quote:caching will only work if you are generating display for your controller with a view.

So how can I trigger a caching after processing the output?
#2

[eluser]Clooner[/eluser]
I use a custom caching helper: http://ellislab.com/forums/viewthread/127155/
#3

[eluser]louis w[/eluser]
Could you just pass your compressed html into another view and output it through that? Then I think it would be cached?

Code:
class MyController extends Controller {
    function myMethod() {

        $html = $this->load->view('myview', '', true);
        $html = compressHtml($html);

        $this->output->cache(1);

        $this->load->view('output', array('html'=>$html));

    }
}

Inside views/output.php just <?php echo $html; ?>

It's kind of a hackish way to accomplish this but would allow you to use the native caching library (i think).
#4

[eluser]gminuses[/eluser]
To clooner:
It seems to be a little overkill, but If I'm running out methods, I'll try that. Thanks.


To louis w:
This might not be a solution. If I have multiple html files to be compressed, I'll have to create this kind of view for every single of them, which is tedious and unmaintainable. Anyway, thanks for the reply.
#5

[eluser]louis w[/eluser]
Couldn't you just use the same output view for all the pages? All it's doing is echoing what you feed it.
#6

[eluser]louis w[/eluser]
Have you also tried instead of your original echo? I don't use the CI cache mechanism but it looks like this might work because it get's written with the output->_display method.

$this->output->set_output($html);
#7

[eluser]gminuses[/eluser]
[quote author="louis w" date="1255550364"]Couldn't you just use the same output view for all the pages? All it's doing is echoing what you feed it.[/quote]
I thought if I use same view for all pages, once it got cached, the following pages won't be displayed, because the view is already cached. Now I figure I might be wrong?

[quote author="louis w" date="1255550505"]Have you also tried instead of your original echo? I don't use the CI cache mechanism but it looks like this might work because it get's written with the output->_display method.

$this->output->set_output($html);[/quote]
This works like a charm, Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB