PDA

View Full Version : Template System Change?


10-20-2000, 04:37 AM
I was experimenting with reading template from disk instead of the database. Following is my instructions on how to do this if you want to try it on your forum and see if it helps or makes it worse. Let me know if you try it, it does work as it is running on my forum at the moment but I can't say if it helped as my forum is small.

first
Make a directory called templates under your forums directory. If you don't have access to write files from php you will need to do this also:

chmod 777 templates

Next place this code in a file, temp.php and run it from your browser. It will take your templates and place them in files in the templates directory. Make sure to run it from your forums directory


<?
$tempdir = "templates/";

require ("global.php");

$query = $DB_site->query("SELECT title, template FROM template");
while ($q = $DB_site->fetch_array($query))
{
$template = $tempdir . $q[title] . ".inc.php";
$fp = fopen($template, "w");
if ($fp)
{
fwrite($fp, $q[template]);
echo "Created $q[title].inc.php<br>";
}
else
{
echo "Error opening $q[title].inc.php<br>";
}
}


NEXT

Edit global.php

Look for ###################### Start gettemplate #######################

Remove everything between that line and this line
###################### Start verifyid #######################

In between those two lines place in :


function gettemplate($templatename,$escape=1) {
// gets a template from the db or from the local cache
global $templatecache,$DB_site;

if (!$templatecache[$templatename])
{
$template = "templates/" . $templatename . ".inc.php";
$x = error_reporting();
error_reporting (0);
$file = fopen($template, "r");
$rf = fread($file, filesize ($template));
if(!$rf)
{
$template = "Error Opening Template $templatename.inc.php";
}
else
{
$template = $rf;
fclose($file);
}
error_reporting ($x);
if ($escape==1) {
$template=str_replace("\"","\\\"",$template);
}
$templatecache["$templatename"]=$template;
}
else
{
$template = $templatecache[$templatename];
}
return $template;
}


NEXT
Look for

// ###################### Start templates #######################
//prepare default templates **********************
$temps=$DB_site->query("SELECT template,title FROM template WHERE title='cssinclude' OR title='header' OR title='footer' OR title='fivelinks' OR title='jumpforumbit' OR title='forumjump'");
while ($temp=$DB_site->fetch_array($temps)) {
$templatecache["$temp[title]"]=$temp[template];
}


Remove it or comment it all out.

Let me know!

10-20-2000, 03:22 PM
Hello Rangersan,

Thanks.

A couple of questions:

1. Right now I'm using Stallions DOSR.php template hack that writes all the templates to the .php scripts on execution. Is this hack faster? Better? why?

2. If I use your hack and update my templates through the web browser admin terminal, do I have to run the temp.php script again to copy the templates to the template directory?

Thanks again. :)

10-20-2000, 05:00 PM
1. I have not seen that hack so I have no comment.

2. Yes you would have to run temp.php or just edit the templates files in the template directory.

3. I don't know if this will help or hurt performance so back up global.php before you try it out.

10-20-2000, 05:03 PM
sorry...Stallion calls it his "Template Opt 1.0"

10-20-2000, 09:00 PM
it's faster to have them in the actual php file rather than including them from disk.

10-20-2000, 10:42 PM
Doron yes it would be but you can't have templates if they are to be stored in the php files themselves, kind of throws the whole template idea out the window.

Jim your referring to the template cache hack? I incorporated cacheing into this one also. Again I didn't say it would help, it would just be interesting to see if it is faster or slower on a big forum.

The question is: Is it quicker to read the template from disk or to retrieve the template via SQL. Does this change when 100 people are grabbing the template from disc vs 100 people grabbing the template from MySql.

10-21-2000, 07:07 PM
the topti hack does just that - it manually adds the new templates into the php files.

10-21-2000, 08:57 PM
Excuse me Doron but I assume you are talking about this hack :
http://www.vbulletin.org/hacks/hacks.php?action=gethack&id=27


For any page that includes global.php this hack loads all the templates and places them in an array that is called using the gettemplate function.

Your usage of the verbage "it's faster to have them in the actual php" implies that the templates are not ever read from anything as they are stored in the actual PHP vBulletin files which, obviously, is not the case.

If you don't understand my hack functions EXACTLY like stallions hack except instead of loading all the templates for every new page, which is what is done whenever global.php is called. It only loads the templates needed for that page from disk and caches any template that is needed more than once (i.e. forumbit, postbit, etc).

The question I was looking at is: is it quicker to A. load all templates into a cache via mysql as topti does or is it quicker to include only the templates from disk that are needed for each page and cache them if they are used more than once.

It was only meant as a case study but Doron you have hopelessly confused everybody as to what it does vs. what topti actually does.

[Edited by rangersfan on 10-21-2000 at 06:02 PM]

10-22-2000, 04:13 AM
Actually :)

That hack is tOpti 2.0.

VirtueTech has tOpti 1.0, which actually stores the templates (HTML code and all) directly inside the .php files. When you update your templates (from the web admin, as normal) you just need to run a file, and all the SQL calls are once again replaced with the HTML.

It has yet to be released, since he encountered a small bug regarding JavaScript code being escaped, but I will get around to it. As far as which hack is faster, I doubt it can get any faster than the way 1.0 handles it, but there's plenty of room to make it "cleaner". :)

So, Doron was right in talking about tOpti 1.0, and you were right talking about tOpti 2.0.

10-22-2000, 04:58 AM
Well yes putting the templates into the actual php file would be the quickest way to do it and it all makes sense now.

It doesn't really matter to me because if you only have a maximum of 12 users online ever then these issues are unimportant to one :)