Created
February 17, 2017 15:55
-
-
Save victorknust/32de3cea971e497fa8d47b7104a6b101 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// | |
$view = new View(); | |
$view->addGlobal('global', 'Hello!, I\'m a Global var.'); | |
// in the method | |
echo $view->render('controller.method.phtml', ['var' => 'Hello World!']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class View | |
{ | |
protected $template; | |
private $globals = array(); | |
private $parameters = array(); | |
public function getGlobals(): array | |
{ | |
return $this->globals; | |
} | |
public function addGlobal( $name, $value ) | |
{ | |
$this->globals[$name] = $value; | |
} | |
public function render( $name, array $parameters = array() ) { | |
$storage = $name; | |
$parameters = array_replace($this->getGlobals(), $parameters); | |
$content = $this->evaluate($storage, $parameters); | |
return $content; | |
} | |
public function include( $name, array $parameters = array() ) | |
{ | |
echo $this->render( $name, $parameters ); | |
} | |
protected function evaluate( $template, array $parameters = array() ) | |
{ | |
$this->template = $template; | |
$this->parameters = $parameters; | |
unset($template, $parameters); | |
extract($this->parameters, EXTR_SKIP); | |
$this->parameters = null; | |
ob_start(); | |
require $this->template; | |
$this->template = null; | |
return ob_get_clean(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment