0

I'm 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 doesn't write out the variables.

I include the function on top of all pages.

1
  • Just to note that "include" is not a function. It is a language construct. Commented Jan 28, 2009 at 16:17

5 Answers 5

2

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)
    return include($file);
}

getTemplate('news', $vars);

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

Sign up to request clarification or add additional context in comments.

6 Comments

why would somebody be doing this way too much code, if it's just one line with $GLOBALS or a global var definition? ridiculous.
Because globals are spawn-of-the-devil evil, that's why. my.opera.com/zomg/blog/2007/08/30/globals-are-evil
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.
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.
In this example, there is just one variable, but one can expect there will be more.
|
1

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.

2 Comments

Smarty = not good. A templating system for a language that is perfect for templating... Mmm. Let me think about that...
@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.
0

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..
}

3 Comments

Globals are considered evil. You should avoid them as much as possible.
that's the explanation, ike, not your ridiculous extract mechanism.
You give a good explenation, i give a good solution. This way you don't have to polute the global scope.
0

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.

Comments

0

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.