Variables from Array in Template System

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikelbring
Forum Commoner
Posts: 38
Joined: Sat Jan 05, 2008 5:28 pm

Variables from Array in Template System

Post by mikelbring »

Code: Select all

<? 
function fetch_template($template,$data = null){ 
 
    eval('?'.'>'.trim($template).'<'.'?'); 
     
} 
 
 
$templatequery = $mysql->query("SELECT * FROM ".DBPREFIX."templates ORDER by name ASC"); 
while ($templ = mysql_fetch_array($templatequery,MYSQL_ASSOC)) 
{ 
 
    $template[$templ['name']] = $templ['body']; 
 
} 
 
$data['title'] = "Site Name"; 
 
fetch_template("header",$data); 
?>
I saw this in a framework I was studying. How would I display the title in the header template with out echoing $data['title']. The framework did echo $title. How would I make all variables come from the $data array?
mikelbring
Forum Commoner
Posts: 38
Joined: Sat Jan 05, 2008 5:28 pm

Re: Variables from Array in Template System

Post by mikelbring »

I figured it out. I had to put extract($data) in the function.
Post Reply