Hello.
I am currently trying to make myself a simple template engine, for the sake of learning.
I have a table i my db with the following tables:
Code:
t_id, t_name, t_content
to fetch a template, all I have to do is to use a simple query in a function:
PHP Code:
// this code is placed in a file called templates.php
function template ($template_name) {
$sql = "SELECT `t_content` FROM `templates` WHERE `t_name` = '$template_name'";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_object($result))
{
echo $row->t_content;
}
}
So wherever I wanna display one of my templates I simply have to include the template file and then put in this simple code:
PHP Code:
template(template-name-here);
Nice and easy. My problem is though; in my templates there are several variables, such as $title, $date, $user, $content etc.. To replace these variable with its actual content I use:
but... the content of these variables are called from other tables, in some cases a table called "news" (i have a news script), and in other cases from other tables. So to summarize....Depending on which template i want to use, different tables should be called and replace the variables in the template.
Is there a good way to do this? I cant for the world understand how this would work. Im hoping for some sort of automated script which replaces the right stuff in the right template, calling the right tables etc. just by printing a function with some values or something. Any help would be greatly appreciated!
Thanks in advance
Niklas