Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by {author} )

Revision: Sparks

Revision from: 13:08, 5 Jul 2009

Sparks: The caching view library

Goals

Sparks is a view library designed to solve two problems:

1) Fine grained cacheablity. Caching in CI is an all or nothing affair. Sparks gives you the ability to divide your PHP blocks into those that are cacheable and those that are not. For example: Many sites display “You are logged in as XXX” in the top corner. This username must be regenerated for each view (from the session data), however the rest of the page may be cacheable.

2) Including views within views (parts). Also automatically wrap view files in “wrapper templates”.

Usage

Write your templates with the following syntax:

<?php echo('This block is cachable'); ?>
<$?php echo('This block is not cachable'); ?$> 

Thus, non-cacheable PHP blocks have an extra $ in the start and end tags.

Your controller will have the following structure:

1) Load libraries and helpers for non-cacheable content
2) Calculate non-cacheable content
3) Check for a current cached version of the template. If it exists, display it and return.
4) Load libraries and helpers for cacheable content.
5) Calculate cacheable content.
6) Render page to cache.
7) Display page.

Also, within your templates you can use the sparks helpers:

<html>
<
body>
<?php sparks_include('another/view/file.php');?>
</body>
</
html

This inserts a ‘part’ into the view.

Example

Controller:

class Test extends Controller {
    
    
function index()
    
{
        $this
->load->library('Sparks');
        
//$this->import->library('com.purplesagelabs.template.Sparks');
        
        //     Set template and content files. Set time to live to 5 seconds.
        
        
$this->sparks->template('template.php')->content('content.php')->ttl(5);
        
        
//    Calculate data for non-cacheable content
        
        
$this->sparks->set('username''Al James'); //Would be get username from session or whatever
        
$this->sparks->set('view_time'time());
        
        
// Check for cached version of page, if its there display it and return
        
        
if ($this->sparks->is_cached()) return $this->sparks->display();
        
        
//Otherwise, generate cacheable content
        
        
$this->sparks->set('headline''Welcome to Sparks!');
        
$this->sparks->set('generate_time'time());
        
        
// Store and display the page
        
        
$this->sparks->store()->display();
        
    
}

Template view file: (wrapper)

<html>
<
body>

    <
h1>Template header!</h1>
    
    
<?php sparks_content(); ?>

</body>
</
html

Content page view file:

<h4>Content fileTodays headline<?$headline ?></h4>

Username <$?= $username ?$> <br/>
Cache file generated at<?$generate_time ?>this page generated at: <$?= $view_time ?$> 

This example demonstrates automatically wrapping files with a ‘wrapper template’. The default template can be configured from config/sparks.php if you want.

What actually happens:

In the first pass, when the cache file is written, the php view file is eval’ed and the <$? ?$> tags are rewritten as <? ?> tags. I.e. the cache file for the example is:

<html>
<
body>

    <
h1>Template header!</h1>
    
<
h4>Content fileTodays headlineWelcome to Sparks!</h4>

Username <?$username ?> <br/>
Cache file generated at11343243255this page generated at<?$view_time ?> 

</body>
</
html

Viewing the cache file simply includes this file.

Anyway, please let me know what you think in the Forum post

File:Sparks.zip
File:Sparks.v1.1.zip

Category:Libraries -> Caching

Categories: