Last active
December 15, 2015 00:29
-
-
Save sbward/5173600 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
namespace MyApplication\Controller; | |
use Hydrogen\Controller; | |
use Hydrogen\MultiTemplateRenderer; | |
class Error extends Controller | |
{ | |
use MultiTemplateRenderer; | |
public function error($message = "Server Error", $httpCode = 500) | |
{ | |
// Checks for CLI automatically | |
// Uses a config variable to map to template file types | |
// (eg. CLI => ".cli.mustache") | |
$this->render('error', compact($message, $httpCode)); | |
return $this->response; | |
} | |
} |
This file contains hidden or 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 | |
namespace Hydrogen; | |
trait MultiTemplateRenderer | |
{ | |
protected function render($path, array $vars = []) | |
{ | |
// This part is still in the air | |
$config = $this->__factory->Config->TemplateRenderer; | |
$tr = $this->__factory->Hydrogen->TemplateRenderer; | |
// Optionally modify the path based on config | |
$realPath = $config->templatePath ?: __APP__.'/Template'; | |
$realPath .= $path; | |
$map = [ | |
Request::INTERFACE_CLI => 'cli', | |
Request::INTERFACE_HTTP => 'php_html' | |
]; | |
$extension = $this->request->interface ?: Request::INTERFACE_HTTP; | |
$extension = $map[$extension]; | |
$extension = $config->extensions->$extension; | |
$realPath .= ".$extension"; | |
$this->response->setBody($tr->render($realPath, $vars)); | |
return $this->response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment