1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82:
<?php
namespace Baguette\Response;
class TwigResponse implements ResponseInterface
{
const CONTENT_TYPE_HTML = 'text/html; charset=utf-8';
protected static $twig;
public $tpl_name;
public $params;
public $content_type;
public $status_code;
public function __construct($tpl_name, array $params = [], $status_code = 200)
{
$this->tpl_name = $tpl_name;
$this->params = $params;
$this->status_code = $status_code;
}
public function getResponseHeaders()
{
return [
'content-type' => [self::CONTENT_TYPE_HTML],
];
}
public function render(\Baguette\Application $app)
{
$params = $this->params + [
'server' => $app->server,
'cookie' => $app->cookie,
'get' => $app->get,
'post' => $app->post,
'now' => $app->now,
];
return self::$twig->render($this->tpl_name, $params);
}
public function getHttpStatusCode()
{
return $this->status_code;
}
public static function setTwigEnvironment(\Twig_Environment $twig)
{
self::$twig = $twig;
}
}