Date: 01/05/01
- Next message: Marko Kaening: "Re: [phplib] Problem with newly commited user4.inc"
- Previous message: Peter Bowyer: "Re: [phplib] Inserting PHP Files using Templates"
- In reply to: Peter Bowyer: "Re: [phplib] Inserting PHP Files using Templates"
- Next in thread: Brian Popp: "Re: [phplib] Inserting PHP Files using Templates"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Fri, Jan 05, 2001 at 04:25:07PM +0000, Peter Bowyer wrote:
> I have been doing this, but it seems to make my site very slow to
> load. Anyone else tried it? I'm still searching for a way to do it
> faster, and the method described in Fred Yankowski's tutorial may work
> better. But can anyone tell me how I could split the code in his article
> into two functions, one to do the header and one to do the footer?
Attached, for your amusement, is a utility class that I wrote to
handle templating for a particular application. It has a lot of cruft
that you probably don't care about, but it does show to create
functions similar to what you're asking for. Note that header and
footer sections are output by header() and footer() method functions,
respectively, and the default constructor calls the header() method
since all my pages start with the header ;-)
This particular case is for a template that actually has four fixed
blocks -- header, middle, end, footer -- providing three intervening
places for content, corresponding (in order) to two table cells and a
final top-level content area. The "usage example" in the initial
comment doesn't reflect that structure.
-- Fred Yankowski fred <email protected> tel: +1.630.879.1312 Principal Consultant www.OntoSys.com fax: +1.630.879.1370 OntoSys, Inc 38W242 Deerpath Rd, Batavia, IL 60510, USA--
<?php // Facade for the PHPlib Template class.
// This class uses a single template that encompasses the entire // resulting page, as opposed to separate templates for header and // footer.
// Usage example: // // < ?php // include("foopage.php3"); // $t = new FooPage("Some page title string", // array("Some category name")); // ? > // some long HTML content section... // < ? $t->footer(); ? >
// Assume php3_include_path will find the PHPlib template.inc file. // We depend on version 1.13 or later of template.inc. include("template.inc");
class FooPageBase { var $tpl; // handle to Template
var $separator = " > "; // item separator in navigation line var $template_path = array("templates", "../templates"); var $mtime = 0;
function FooPageBase($title, $page_name, $template) { global $HTTP_GET_VARS; $this->do_stats_p = isset($HTTP_GET_VARS['stats']); if ($this->do_stats_p) $this->start = $this->utime();
$this->find_templates(); $this->tpl = new Template($this->template_path, "keep");
$this->tpl->set_file('page', $template); $this->tpl->set_var(array('title' => $title, 'page_name' => $page_name)); if (is_array($other_vars)) { while (list($key, $value) = each($other_vars)) { $this->tpl->set_var($key, $value); } } }
function reply_304_if_not_modified() { global $HTTP_IF_MODIFIED_SINCE; $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); if (! $if_modified_since) return; $mtime = $this->get_mtime(); $gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; if ($if_modified_since == $gmdate_mod) { header("HTTP/1.0 304 Not Modified"); exit; } }
function get_mtime() { global $SCRIPT_FILENAME; if (! $this->mtime) $this->mtime = filemtime($SCRIPT_FILENAME); return $this->mtime; }
function print_block($name) { $this->tpl->set_block('page', $name); $this->tpl->pparse('out', $name); }
function http_headers() { #$this->reply_304_if_not_modified(); return; // stub off if ($mtime = $this->get_mtime()) { $date_mod = gmdate("D, d M Y H:i:s", $mtime); header("Last-Modified: $date_mod GMT"); } }
function header() { $this->print_block('header'); flush(); }
function middle() { $this->print_block('middle'); flush(); }
function end() { $this->print_block('end'); flush(); }
function footer() { $this->set_last_mod(); $this->print_block('footer'); $this->report_stats(); }
function set_last_mod() { $mtime = $this->get_mtime(); $mod_date = gmdate('Y-m-d H:i \Z', $mtime); $this->tpl->set_var('last_mod', $mod_date); }
function report_stats() { if ($this->do_stats_p) printf('<p><span class="stats">Completed in %s seconds.</span><br>', $this->utime() - $this->start); //phpinfo(); }
function utime() { // return current time expressed as microseconds $time = explode(" ", microtime()); $usec = (double)$time[0]; $sec = (double)$time[1]; return $sec + $usec; }
function find_templates() { // Prune non-existent directories from $this->template_path $new_path = array(); while (list($_, $dir) = each($this->template_path)) { if (is_dir($dir)) { $new_path[] = $dir; } } $this->template_path = $new_path; } }
class FooPage extends FooPageBase { // This subclass automatically outputs the 'header' block when the // instance is created, to simplify the most common client case. function FooPage($title, $page_name, $template="page1.html") { $this->FooPageBase($title, $page_name, $template); $this->http_headers(); $this->header(); } } ?>
--
--------------------------------------------------------------------- To unsubscribe, e-mail: phplib-unsubscribe <email protected> For additional commands, e-mail: phplib-help <email protected>
- Next message: Marko Kaening: "Re: [phplib] Problem with newly commited user4.inc"
- Previous message: Peter Bowyer: "Re: [phplib] Inserting PHP Files using Templates"
- In reply to: Peter Bowyer: "Re: [phplib] Inserting PHP Files using Templates"
- Next in thread: Brian Popp: "Re: [phplib] Inserting PHP Files using Templates"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

