Skip to content

Instantly share code, notes, and snippets.

@victorknust
Created February 17, 2017 15:55
Show Gist options
  • Save victorknust/32de3cea971e497fa8d47b7104a6b101 to your computer and use it in GitHub Desktop.
Save victorknust/32de3cea971e497fa8d47b7104a6b101 to your computer and use it in GitHub Desktop.
<?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!']);
<?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