| |
|
 |
Page caching with templates
|
The createPage() function is listed below. I have made it as simple as possible to demonstrate the topic in focus, page caching. As you see from the code we use templates as usual, and if $generateStaticPage is false the template is parsed as it normally does. The special bit about this code is when $generateStaticPage is set to true. A file is then opened for write and the template is parsed out to a variable, $output. This $output is then printed to the client and stored in a file.
function createPage( $generateStaticPage, $cachedFile = "" )
{
include_once( "template.inc" );
$t = new eZTemplate( "templates/" );
$t->set_file( "page_tpl" => "page.tpl" );
// perform calculations
$t->set_var( "template_variable", $value );
if ( $generateStaticPage == true )
{
$fp = fopen( $cachedFile, "w+" );
$output = $t->parse( $target, "page_tpl" );
// display the page contents and write it to file
print( $output );
fwrite ( $fp, $output );
fclose( $fp );
}
else
{
$t->pparse( "output", "page_tpl" );
}
} |
The next time this page is visited the pure static version of the page is served and you don't get so much load on your server.
Simple isn't it?
Comment List
There are no comments.
|
 |
|
|