tags:

views:

124

answers:

5

Im trying to make a simple template system in PHP. I'm new to PHP, so it's nothing serious. I have some problems though:

A regular include works:

$variable = "test";
include("templates/news.html");

But this won't:

This says $variable is undefined:

$variable = "test";
getTemplate("news");

The Function:

function getTemplate($tpl) {
    $file = "templates/$tpl.html";
    if (file_exists($file))
        return include($file);
    return false;
}

news.html

<h1>php echo $variable</h1>

the function works and includes the page but it dont write out the variables

I include the function on top of all pages.

Thanks in advance!

+1  A: 

you are trying to reach global variable in a function.

you must either declare a variable global in a function or just use: $GLOBALS["variable"]

function () {
global $variable;
etc..
}
dusoft
Globals are considered evil. You should avoid them as much as possible.
Ikke
Still, that IS why this doesn't work.
jacobangel
that's the explanation, ike, not your ridiculous extract mechanism.
dusoft
You give a good _explenation_, i give a good _solution_. This way you don't have to polute the global scope.
Ikke
A: 

Because you're including the file from inside a function, the contents of that file are no longer in the global scope.

You either need to add global $variable; to the start of the function, or use echo $GLOBALS['variable']; in news.html.

Greg
A: 

I believe your problem has to do with variable scoping. When you call a function, you are unable to access variables outside of that function, except global variables (more on this in a second) or variables passed to your function. The global function tells PHP that you'd like to use the same variable in the function as in the parent scope. You can also use the define function to define constants for your script, which are universally accessed.

Using large number of global variables is very frowned upon, I'd suggest passing either an associative array to the function, or use a more object oriented approach.

Hope this helps, Jacob

Ranok
Okay, that's just freaky... Why are you hoping it will help me?! I haven't even said anything yet!
Jacob
+2  A: 

With the extract function, you can define different variables from an array.

You can make it like this:

$vars = array('var1' => "value1", 'var2' => "value2");

function getTemplate($tpl, $vars) {
$file = "templates/$tpl.html";

extract($vars, EXTR_SKIP)

if (file_exists($file))
    return include($file);
return false;
}

getTemplate('news', $vars);

In your template, you can use $var1 and $var2.

Ikke
why would somebody be doing this way too much code, if it's just one line with $GLOBALS or a global var definition? ridiculous.
dusoft
Because globals are spawn-of-the-devil evil, that's why. http://my.opera.com/zomg/blog/2007/08/30/globals-are-evil
Jacob
Because you have to make a global line for every var, or refer to $global for every var. This works for as many variables you have, and you can just use the variable names.And because of what Jacob said.
Ikke
that's just one global for your sake. and even that can be completely change to class variable and the no global will be used.
dusoft
In this example, there is just one variable, but one can expect there will be more.
Ikke
The extract() function should be called with $extract_type EXTR_SKIP or some other than EXTR_OVERWRITE to prevent $file to be overwritten.
Gumbo
A: 

Have you considered using Smarty

If you don't want to use this template engine maybe you should check some of the ideas behind it. In short, it deals with assigning variables to templates by assigning variable values to an object. If you assigned variable to a template object you can then use it in template file.

empi
Smarty = not good. A templating system for a language that is perfect for templating... Mmm. Let me think about that...
Paolo Bergantino
@Paolo Bergantino: in my opinion it works great when using MVC separation. using smarty creates clear separation between application logic and presentation. what is more, for xhtml people it's much easier to understand smarty template than php file.
empi