Page 1 of 1

My Template System

Posted: Mon May 19, 2008 5:28 pm
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.

Re: My Template System

Posted: Tue May 20, 2008 11:41 pm
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

Re: My Template System

Posted: Wed May 21, 2008 12:38 am
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.

Re: My Template System

Posted: Wed May 21, 2008 1:46 am
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?

Re: My Template System

Posted: Wed May 21, 2008 2:55 am
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.

Re: My Template System

Posted: Wed May 21, 2008 12:23 pm
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