My 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

My Template System

Post by mikelbring »

I have a problem with this template system I made. First let me walk you through it and how it works so I can get to my problem.
First I have a table called templates right with: id, name, body.
in my global file which is the file included into the top of every page I run this code.

Code: Select all

 
$fetch_templates = $mysql->query("SELECT * FROM ".DBPREFIX."templates ORDER by id ASC");
while ($temp = mysql_fetch_array($fetch_templates,MYSQL_ASSOC)){
 
    $template[$temp['name']] = $temp['body'];
 
}
 
I dump all my templates into an array every page. Now when I want to show a template that has php in it I do.

Code: Select all

 
$core->display_template($template['test']);
 
The template name is: test. Here is my display_template function:

Code: Select all

 
public function display_template($v){
    
    global $SDK;
    global $mysql;
    global $error;
    global $ipb;
        
    eval('?'.'>'.$v.'<'.'?');   
    
}
 
For some reason say I have the following code in the test template:

Code: Select all

 
<? echo $testvar; ?>
 
and then a file with this in it:

Code: Select all

 
$testvar = "mike";
$core->display_template($template['test']);
 
Some reason I cannot get the $testvar to work with the template. I have tried it with out running the display_template function as well. Any help or suggestions would be really appreciated. Thanks.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Re: My Template System

Post by andre_c »

eval "starts" php for you, you don't need the '<?php' and '?>' tags. So what you actually want on the template is:

Code: Select all

echo $testvar;
not

Code: Select all

<? echo $testvar; ?>
Alvaro
mikelbring
Forum Commoner
Posts: 38
Joined: Sat Jan 05, 2008 5:28 pm

Re: My Template System

Post by mikelbring »

That's not true, a lot of my templates have html in them. So I want the php to start when I set the <??> tags.

Edit: Either way it wouldn't output the variable.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Re: My Template System

Post by andre_c »

Sorry, I just realized that in your call to eval you ARE closing the php tag.

Are you getting any errors, warnings, or notices?
mikelbring
Forum Commoner
Posts: 38
Joined: Sat Jan 05, 2008 5:28 pm

Re: My Template System

Post by mikelbring »

No i am not, php works fine I just cant call a variable that I defined before I set the template. The template shows and everything just predefined variables don't.
User avatar
puke7
Forum Newbie
Posts: 12
Joined: Sat May 10, 2008 11:49 am

Re: My Template System

Post by puke7 »

your problem is encapsulation
when calling eval in your template function it has no way of knowing the value of $testvalue
perhaps you could set up an array with all necessary values needed by templates that you could then call global in inside your function
Post Reply