-
Notifications
You must be signed in to change notification settings - Fork 4
Sparks
Goals
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".
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:
- 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:
<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 file. Todays 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 file. Todays headline: Welcome to Sparks!</h4>
Username <?= $username ?> <br/>
Cache file generated at: 11343243255, this 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