View Full Version : templates via a database
Dimava
Fri 13th Sep '02, 11:34pm
I want to store all my template files in a mysql database, my only question is how do I take the templates from the db, so that the $variables equal what they are set to in the code.
I know that vbulletin uses something like eval, but i'm not sure how that works
thanks,
Dimava
Dan615
Fri 20th Sep '02, 10:22pm
function template($name, $escape = 1) {
global $template_cache;
if (isset($template_cache[$name]) and $template_cache[$name != '') {
$template = $template_cache[$name];
} else {
$temp_res = @mysql_query("select template from templates where name = '$name'") or die(mysql_error());
$temp_arr = mysql_fetch_array($temp_res);
$template = $temp_arr['template'];
$template_cache[$name] = $template;
}
if ($escape) {
$template = str_replace("\"", "\\\"", $template);
}
return $template;
}
This assumes you have a template cache array defined somewhere in your script.
Dimava
Fri 20th Sep '02, 11:20pm
thanks for the code, and whats a template cache array?
thanks
Dimava
Dan615
Sat 21st Sep '02, 9:10pm
the template cache keeps a copy of the template in an array for fast access later if it's needed. it is usually used along with another function that preloads the templates into the cache:
function trim_array($array) {
reset($array);
while (list($key,$val) = each($array)) {
if (is_array($array[$key])) {
$array[$key] = trim_array($array[$key];
} else {
$array[$key] = trim($array[$key]);
}
}
return $array;
}
function preload_templates($list) {
global $template_cache;
$temps = explode(',', $list); // seperates the words by commas
$temps = trim_array($temps); // remove extra spaces
if (count($temps) == 1) {
$temp_result = @mysql_query("select name, template from templates where name = '$temps[0]'") or die(mysql_error());
$temp = mysql_fetch_array($temp_result);
$template_cache[$temp['name']] = $temp['template'];
} else {
$query = 'select name, template from templates where '; // start building query
for ($i = 0; $i < count($temps); $i++) {
$query .= "name = '$temps[$i]' or ";
}
$query = substr($query, 0, strlen($query) - 4); // chop off extra 'or'
$temp_result = @mysql_query($query);
while ($temp = mysql_fetch_array($temp_result)) {
$template_cache[$temp['name']] = $temp['template'];
}
}
}
vBulletin® v3.8.0 Release Candidate 1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.