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

[size=4]Sparks: The caching view library[/size]

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:

&lt;html&gt;
&lt;body&gt;
&lt;?php sparks_include('another/view/file.php');?&gt;
&lt;/body&gt;
&lt;/html&gt;

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)

&lt;html&gt;
&lt;body&gt;

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

&lt;/body&gt;
&lt;/html&gt; 

Content page view file:

<h4>Content file. Todays headline: &lt;?= $headline ?&gt;</h4>

Username <$?= $username ?$> <br/>
Cache file generated at: &lt;?= $generate_time ?&gt;, 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:

&lt;html&gt;
&lt;body&gt;

    <h1>Template header!</h1>
    
<h4>Content file. Todays headline: Welcome to Sparks!</h4>

Username &lt;?= $username ?&gt; <br/>
Cache file generated at: 11343243255, this page generated at: &lt;?= $view_time ?&gt; 

&lt;/body&gt;
&lt;/html&gt; 

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

Clone this wiki locally