-
Notifications
You must be signed in to change notification settings - Fork 0
Sparks
[size=4]Sparks: The caching view library[/size]
[b]Goals[/b]
Sparks is a view library designed to solve two problems:
-
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.
-
Including views within views (parts). Also automatically wrap view files in "wrapper templates".
[b]Usage[/b]
Write your templates with the following syntax:
[code] <?php echo('This block is cachable'); ?> <$?php echo('This block is not cachable'); ?$> [/code]
Thus, non-cacheable PHP blocks have an extra $ in the start and end tags.
Your controller will have the following structure:
- Load libraries and helpers for non-cacheable content
- Calculate non-cacheable content
- Check for a current cached version of the template. If it exists, display it and return.
- Load libraries and helpers for cacheable content.
- Calculate cacheable content.
- Render page to cache.
- Display page.
Also, within your templates you can use the sparks helpers:
[code] <html> <body> <?php sparks_include('another/view/file.php');?> </body> </html> [/code]
This inserts a 'part' into the view.
[b]Example[/b]
Controller:
[code] 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();
}
} [/code]
Template view file: (wrapper)
[code] <html> <body>
<h1>Template header!</h1>
<?php sparks_content(); ?>
</body> </html> [/code]
Content page view file:
[code]
Username <$?=
Cache file generated at: <?=
This example demonstrates automatically wrapping files with a 'wrapper template'. The default template can be configured from config/sparks.php if you want.
[b]What actually happens:[/b]
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:
[code]
<html> <body>
<h1>Template header!</h1>
Username <?= $username ?>
Cache file generated at: 11343243255, this page generated at: <?= $view_time ?>
</body> </html> [/code]
Viewing the cache file simply includes this file.
Anyway, please let me know what you think in the [url=http://codeigniter.com/forums/viewthread/56456/]Forum post[/url]