Reputation: 7492
I have two files:
template.php:
<div><?php echo $myVar; ?></div>
controller
class MyClass extends Base {
public function view() {
$myVar = 'hello world';
$this->loadTemplate('main');
}
}
abstract class Base {
public function loadTemplate($template) {
require_once("templates/$template.php");
}
}
but this must not works because the require is in scope of loadTemplate function, how can i return the call to require function in the loadTemplate? i want the require be included in the view() scope using single function like $this->loadTemplate(); and not using require_once($this->getTemplatePath())
any idea?
Upvotes: 0
Views: 81
Reputation: 12721
simply use class members to access the variables in your viewscript
Classes
class MyClass extends Base
{
public function view()
{
$this->myVar = 'hello world';
$this->loadTemplate('main');
}
}
abstract class Base
{
public function loadTemplate($template)
{
require_once("templates/$template.php");
}
}
Template main.php
Output: <?= $this->myVar ?>
Upvotes: 2
Reputation: 43168
You can't. Generally what you're trying to do is accomplished by passing an array with your variables ($this->loadTemplate(array('myVar' => 'hello world'))
) and then calling extract($vars)
in the loadTemplate
method.
Upvotes: 2