Permalink
Browse files

Adding exception rendering and basic styles.

  • Loading branch information...
1 parent b7eb7ae commit 0ce5b3ae36e7bab4e31850adbdd6d5dcb6cb3636 @paulredmond committed Apr 7, 2012
View
@@ -3,7 +3,11 @@
// TwigPlugin bootstrap file
//
-require dirname(__FILE__) . '/../Vendor/.composer/autoload.php';
+if (!defined('__DIR__')) {
+ define('__DIR__', dirname(__FILE__));
+}
+
+require __DIR__ . '/../Vendor/.composer/autoload.php';
// Override in app/Config/bootstrap.php if needed.
if (!defined('TWIG_CACHE_PATH')) {
@@ -0,0 +1,3 @@
+<?php
+
+class TwigAppController extends AppController {}
View
@@ -0,0 +1,3 @@
+<?php
+
+class TwigAppModel extends AppModel {}
@@ -0,0 +1,4 @@
+<h4>Stack Trace</h4>
+<pre>
+<?php echo $error->getTraceAsString(); ?>
+</pre>
View
@@ -0,0 +1,13 @@
+{% extends 'Twig:Layouts:error.twig' %}
+
+{% block pageTitle %}{{ type|capitalize }} Error | {{ error.message }}{% endblock %}
+
+{% block title %}
+<h1>{{ error.message }}</h1>
+{% endblock %}
+
+{% block error %}
+<div class="error-location">
+ <p>{{ error.file }}, Line: {{ error.line }}</p>
+</div>
+{% endblock %}
View
@@ -1,9 +1,44 @@
-{% block title %}<h2>{{ name }}</h2>{% endblock %}
+<!DOCTYPE html>
+<html>
+<head>
+ <title>{% block pageTitle %}{{ name|raw }}{% endblock pageTitle %}</title>
+ <link rel="stylesheet" type="text/css" href="/twig/css/error.css" />
+</head>
+<body>
-{% block body %}
-Doh!
-{% endblock %}
+<div class="content">
+ <div class="exception">
+ {% block title %}
+ <h1>{{ name|raw }}</h1>
+ {% endblock title %}
-{% block stacktrace %}
-{{ _view.element('exception_stack_trace') }}
-{% endblock %}
+ {% block error %}{% endblock %}
+ </div>
+
+
+ <div class="stacktrace">
+ {% block stacktrace %}
+ <h1>Stack Trace</h1>
+ <ol>
+ {% for i in trace %}
+ <li>at
+ <strong>
+ <abbr title="{{ i.class}}">{{ i.class }}</abbr>
+ {{ i.type }}{{ i.function }}
+ </strong>
+ ()
+ <br />
+ in {{ i.file }} at line {{ i.line }}
+ </li>
+ {% endfor %}
+ </ol>
+ {% endblock %}
+ </div>
+</div>
+
+<div class="copyright">
+ <p>Twig plugin for CakePHP2 by Paul Redmond. Twig <a href="http://twig.sensiolabs.org/images/twig-logo.png">logo</a> is &copy; 2010 Sensio Labs.</p>
+</div>
+
+</body>
+</html>
View
@@ -8,8 +8,8 @@
* @author Paul Redmond <paulrredmond@gmail.com>
* @link https://github.com/paulredmond/cakephp-twig Github
* @link http://goredmonster.com/ Author
- * @package TwigPlugin
- * @subpackage TwigPlugin.View.Twig
+ * @package Twig
+ * @subpackage Twig.View.Twig
* @license MIT
*/
@@ -141,14 +141,40 @@ public function render($view = null, $layout = null)
$relative = str_replace($this->templatePaths, '', $viewFileName);
$relative = ltrim($relative, '/');
+ $this->set('twig_error_layout', 'Twig:Layouts:error.twig');
- $template = $this->TwigEnv->loadTemplate($relative);
- $this->output = $template->render(array_merge($this->viewVars, array('_view' => $this)));
- $this->hasRendered = true;
+ // Render
+ try {
+ $template = $this->TwigEnv->loadTemplate($relative);
+ $this->output = $template->render(array_merge($this->viewVars, array('_view' => $this)));
+ $this->hasRendered = true;
+ } catch(Twig_Error_Syntax $e) {
+ return $this->renderTwigException('Syntax', $e);
+ } catch(Twig_Error_Runtime $e) {
+ return $this->renderTwigException('Runtime', $e);
+ } catch(Twig_Error $e) {
+ return $this->renderTwigException('Twig', $e);
+ }
return $this->output;
}
+ private function renderTwigException($type, Twig_Error $error, $file = 'error')
+ {
+ $e = $error;
+ $template = $this->TwigEnv->loadTemplate("Twig:Errors:{$file}.twig");
+
+ return $template->render(array(
+ 'type' => $type,
+ 'error' => array(
+ 'message' => $e->getMessage(),
+ 'file' => ltrim(str_replace(ROOT, '', $e->getFile()), DS),
+ 'line' => $e->getLine(),
+ ),
+ 'trace' => $e->getTrace(),
+ ));
+ }
+
/**
* Override default View _render method.
* Uses Twig's exception handling for errors.
@@ -323,7 +349,7 @@ private function _twigException($type, $content, $filename, Exception $e)
$type = 'TwigView: ' . $type;
$this->viewVars['title_for_layout'] = $type;
if ($this->debug == true) {
- $this->plugin = 'TwigPlugin';
+ $this->plugin = 'Twig';
echo $this->renderLayout($content, 'twig_exception');
exit; # Important!
} else {
View
@@ -0,0 +1,71 @@
+/* Error / Exception Style */
+
+
+body {
+ background: #fff url('../images/twig-logo.png') no-repeat top right;
+ font: 12px/1.5em "Helvetica", "Arial", "sans-serif";
+ color: #333;
+ width: 1000px;
+ margin: 2em auto;
+}
+
+
+
+.content > div {
+ padding: 10px 15px;
+}
+
+pre {
+ font-family: "Georgia";
+}
+
+.accessibly-hidden {
+ position: absolute;
+ left: -9999px;
+}
+
+.content {
+ background: rgba(238, 238, 238, 0.7);
+}
+
+.exception {
+ fonts-size: 14px;
+ color: #fff;
+ padding: 15px;
+ -webkit-border-radius: 10px 10px 0 0;
+ -moz-border-radius: 10px 10px 0 0;
+ -ms-border-radius: 10px 10px 0 0;
+ border-radius: 10px 10px 0 0;
+ background: #555;
+}
+.exception h1 {
+ font-size: 20px;
+ line-height: 24px;
+}
+
+.exception .error-location {
+ font-size: 16px;
+ font-weight: bold;
+}
+
+.stacktrace {
+ border: 1px solid #999;
+ border-width: 0 1px 1px;
+ -webkit-border-radius: 0 0 10px 10px;
+ -moz-border-radius: 0 0 10px 10px;
+ -ms-border-radius: 0 0 10px 10px;
+ border-radius: 0 0 10px 10px;
+}
+
+.stacktrace ol {
+ color: #888;
+}
+
+.stacktrace strong {
+ color: #333;
+}
+
+.stacktrace li {
+ line-height: 1.75em;
+ margin: 0 0 0.5em;
+}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0ce5b3a

Please sign in to comment.