If you want to do this manually, you can use output buffering.
Example:
static.php:
Hello, <a href="profile.php"><?php echo htmlspecialchars($username); ?></a>!
functions.php:
function cache_page($template,$uid,$vars){
$cache='cache/'.$uid.'-'.md5(@json_encode($vars).'.cache');
if(!file_exists(){
foreach($vars as $name=>$value)$$name=$value;
ob_start();
include_once $template;
file_put_contents($cache,ob_get_clean());
}
readfile($cache);
}
index.php:
require_once 'functions.php';
cache_page(
'static.php',
getUser()->id,
array('username'=>getUser()->username)
);