I'm using the template engine specified below. While everything works well, when I include variables/objects from the main page (e.g index.php) in the template file e.g (main.php), the object cannot be accessed.
The reason I'm doing this is because in my template file, I want to check whether a user is logged in, and display a different header respectively.
But when I attempt to print out a value, I get the error "Notice: Trying to get property of non-object".
The User Account and Template class are both independent.
In my template file:
<html>
<body>
<?php if ($user->isLoggedIn()) { ?>
<HEADER 1>
<?php } else { ?>
<HEADER 1>
<?php } ?>
</body>
</html>
My template engine:
class Template
{
protected $template;
protected $variables = array();
public function __construct($template)
{
$this->template = $template;
}
public function __get($key)
{
return $this->variables[$key];
}
public function __set($key, $value)
{
$this->variables[$key] = $value;
}
public function __toString()
{
extract($this->variables);
chdir(dirname($this->template));
ob_start();
include basename($this->template);
return ob_get_clean();
}
}
index.php
require 'global.php';
require 'templateEngine.php';
$user = new Account(); // the $user object works when I print it on the same page, but not when it's in the template file.
$view = new Template("path/to/template.php");
$view->title = "Hello world!";
$view->description = "This is a ridiculously simple template";
echo $view;
What should I do so that my template file can access my $user object, generated from index.php?
Note: the template engine uses 'Includes', hence the title - but I may be wrong.
Thanks!