Skip to content
This repository has been archived by the owner on Aug 25, 2019. It is now read-only.

Commit

Permalink
Optimalizations in template.php, debug also works without </body> tag…
Browse files Browse the repository at this point in the history
… and more powerful and better output compression method.
  • Loading branch information
dietrichm committed Apr 13, 2004
1 parent 907e7e0 commit 51d90fa
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 49 deletions.
2 changes: 2 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ UseBB 0.3 Changelog
- Fixed bug with MySQL 3.x that gave error on forum view.
- Fixed quick reply, now only visible when the user is actually allowed to reply.
- Fixed plain spaces with &nbsp; between buttons in the footer template.
- Optimalizations in template.php, debug also works without </body> tag and more
powerful and better output compression method.

UseBB 0.2 Changelog
------------------------------
Expand Down
117 changes: 68 additions & 49 deletions sources/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,6 @@ class template {
var $requests;
var $templates;

//
// Constructor, initializes arrays used in this class
//
function template() {

$this->needed = array();
$this->requests = array();
$this->templates = array();

}

//
// Get configuration variables
//
Expand Down Expand Up @@ -85,27 +74,44 @@ function parse($name, $vars=array()) {

global $functions, $lang;

//
// Add this template name
//
$this->needed = ( !is_array($this->needed) ) ? array() : $this->needed;
if ( !in_array($name, $this->needed) )
$this->needed[] = $name;

//
// Add some standard variables
//
$vars = ( !is_array($vars) ) ? array() : $vars;

$vars['img_dir'] = 'gfx/'.$functions->get_config('template').'/';
$vars['lang'] = $functions->get_config('language');

//
// Save the template vars
//
$this->requests = ( !is_array($this->requests) ) ? array() : $this->requests;
$this->requests[] = array(
'name' => $name,
'vars' => $vars
);

}

//
// Replace {page_title} in every template by the given title
//
function set_page_title($title) {

foreach ( $this->requests as $key => $val ) {
if ( is_array($this->requests) && count($this->requests) ) {

if ( is_array($this->requests[$key]['vars']) )
$this->requests[$key]['vars']['page_title'] = $title;
foreach ( $this->requests as $key => $val ) {

if ( is_array($this->requests[$key]['vars']) )
$this->requests[$key]['vars']['page_title'] = $title;

}

}

Expand All @@ -119,44 +125,52 @@ function body() {
global $functions, $db, $timer;

//
// Get all the templates we need
//
foreach ( $this->needed as $val )
$query_where_part[] = "'".$val."'";
$query_where_part = '( '.join(', ', $query_where_part).' )';
if ( !($result = $db->query("SELECT name, content FROM ".TABLE_PREFIX."templates WHERE template = '".$functions->get_config('template')."' AND name IN ".$query_where_part)) )
$functions->usebb_die('SQL', 'Unable to get contents of template "'.$functions->get_config('template').'"!', __FILE__, __LINE__);
while ( $templates = $db->fetch_result($result) )
$this->templates[$templates['name']] = stripslashes($templates['content']);

$body = '';

//
// Build each template
// If there are any templates parsed
//
foreach ( $this->requests as $request ) {
if ( is_array($this->needed) && count($this->needed) ) {

//
// When variables has been passed
// Get all the templates we need
//
if ( is_array($request['vars']) && count($request['vars']) > 0 ) {

//
// Parse the variables and add it to the body
//
if ( !isset($this->templates[$request['name']]) )
$functions->usebb_die('Template', 'Missing template "'.$request['name'].'"!', __FILE__, __LINE__);
$current_template = $this->templates[$request['name']];
foreach ( $request['vars'] as $key => $val )
$current_template = str_replace('{'.$key.'}', $val, $current_template);
$body .= $current_template."\n";

} else {
foreach ( $this->needed as $val )
$query_where_part[] = "'".$val."'";
$query_where_part = '( '.join(', ', $query_where_part).' )';
if ( !($result = $db->query("SELECT name, content FROM ".TABLE_PREFIX."templates WHERE template = '".$functions->get_config('template')."' AND name IN ".$query_where_part)) )
$functions->usebb_die('SQL', 'Unable to get contents of template "'.$functions->get_config('template').'"!', __FILE__, __LINE__);
$this->templates = array();
while ( $templates = $db->fetch_result($result) )
$this->templates[$templates['name']] = stripslashes($templates['content']);

$body = '';

//
// Build each template
//
foreach ( $this->requests as $request ) {

//
// Just add the template to the body
// When variables has been passed
//
$body .= $this->templates[$request['name']];
if ( is_array($request['vars']) && count($request['vars']) > 0 ) {

//
// Parse the variables and add it to the body
//
if ( !isset($this->templates[$request['name']]) )
$functions->usebb_die('Template', 'Missing template "'.$request['name'].'"!', __FILE__, __LINE__);
$current_template = $this->templates[$request['name']];
foreach ( $request['vars'] as $key => $val )
$current_template = str_replace('{'.$key.'}', $val, $current_template);
$body .= $current_template."\n";

} else {

//
// Just add the template to the body
//
$body .= $this->templates[$request['name']];

}

}

Expand All @@ -179,24 +193,29 @@ function body() {
//
// List parsetime and queries in short
//
$body = str_replace('</body>', '<div align="center"><small>Parse time: '.$parsetime.' - Used templates: '.count($this->needed).' - Used queries: '.count($db->queries).'</small></div></body>', $body);
$debug_output = '<div align="center"><small>Parse time: '.$parsetime.' - Used templates: '.count($this->needed).' - Used queries: '.count($db->queries).'</small></div>';

} elseif ( intval($functions->get_config('debug')) == 2 ) {

//
// Lists parsetime and queries fully
//
$body = str_replace('</body>', '<div><b>Debug mode</b><br />Parse time: '.$parsetime.'<br />Used templates ('.count($this->needed).'): '.join(', ', $this->needed).'<br />Used queries ('.count($db->queries).'):<br /><textarea rows="10" cols="50" readonly="readonly">'.htmlentities(join("\n\n", $db->queries)).'</textarea></div></body>', $body);
$debug_output = '<div><b>Debug mode</b><br />Parse time: '.$parsetime.'<br />Used templates ('.count($this->needed).'): '.join(', ', $this->needed).'<br />Used queries ('.count($db->queries).'):<br /><textarea rows="10" cols="50" readonly="readonly">'.htmlentities(join("\n\n", $db->queries)).'</textarea></div>';

}

if ( preg_match("/<\/body>/i", $body) )
$body = preg_replace("/<\/body>/i", $debug_output.'</body>', $body);
else
$body .= $debug_output;

}

//
// Output compression
//
if ( $functions->get_config('output_compression') )
$body = preg_replace("/>\s+</", '><', $body);
$body = preg_replace("/\s+/", ' ', $body);

echo $body;

Expand Down

0 comments on commit 51d90fa

Please sign in to comment.