Skip to content

Instantly share code, notes, and snippets.

@johnpancoast
Forked from sbward/Error.php
Created March 15, 2013 22:49
Show Gist options
  • Save johnpancoast/5173737 to your computer and use it in GitHub Desktop.
Save johnpancoast/5173737 to your computer and use it in GitHub Desktop.
<?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;
}
}
<?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