Don't mix data model and UI: use template to display debug log
Don't mix data model and UI: use template to display debug log

--- a/css/gitphpskin.css
+++ b/css/gitphpskin.css
@@ -612,12 +612,19 @@
 .debug_toggle {
 	color: #88a; border-bottom: 1px dashed blue;
 }
+
 .debug_key {
 	background: #ccf; border-bottom: 1px solid #888;
 }
+
 .debug_value {
 	background: #ccc; border-bottom: 1px solid #888;
 }
+
+.debug_value .debug_addl {
+	font-style: italic;
+}
+
 .debug_time {
 	background: #cff; border-bottom: 1px solid #888;
 }

--- a/include/DebugLog.class.php
+++ b/include/DebugLog.class.php
@@ -208,6 +208,16 @@
 	}
 
 	/**
+	 * Gets the log entries
+	 *
+	 * @return array entry data
+	 */
+	public function GetEntries()
+	{
+		return $this->entries;
+	}
+
+	/**
 	 * Notify that observable object changed
 	 *
 	 * @param GitPHP_Observable_Interface $object object
@@ -232,46 +242,5 @@
 		$this->Log($msg, $msg_data, $type);
 	}
 
-	public function PrintHtml()
-	{
-		if (!$this->enabled) return;
-
-		foreach ($this->entries as $i => $e) {
-			if (strlen($e['value']) > 512) {
-				$contents  = htmlspecialchars(substr($e['value'], 0, 512) . "...");
-				$contents .= "\n\n<i>" . (strlen($e['value']) - 512) . " bytes more in output</i>";
-			} else {
-				$contents = htmlspecialchars($e['value']);
-			}
-			echo "<tr>
-				<td class='debug_key'>$e[name]</td>
-				<td class='debug_value'>
-					" . nl2br($contents) . ($contents != "" ? "<br§ />" : "") . "
-					<span class='debug_toggle'>trace</span>&nbsp;
-					<div class='debug_bt'>$e[bt]</div>
-				</td>
-				<td class='debug_time'>
-					" . ($e['time'] ? sprintf("%.1f", $e['time'] * 1000) : '') . "
-					" . ($e['time'] ? (!empty($e['reltime']) ? " ms from start" : " ms") : '') . "
-				</td>
-			</tr>";
-		}
-	}
-
-	public function PrintHtmlHeader()
-	{
-		if (!$this->enabled) return;
-
-		echo
-<<<HEREDOC
-		<table class="debug"><tbody>
-HEREDOC;
-	}
-
-	public function PrintHtmlFooter()
-	{
-		if (!$this->enabled) return;
-		echo '</tbody></table>';
-	}
 }
 

--- a/include/controller/ControllerBase.class.php
+++ b/include/controller/ControllerBase.class.php
@@ -652,6 +652,11 @@
 
 		if ($this->projectList)
 			$log->Log('MemoryCache count: ' . $this->projectList->GetMemoryCache()->GetCount());
+
+		if ($log->GetEnabled()) {
+			$this->tpl->assign('debuglog', $log);
+			$this->tpl->display('debug.tpl');
+		}
 	}
 
 	/**

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -100,15 +100,5 @@
 
 unset($router);
 
-if (isset($controller)) {
-	$log = $controller->GetLog();
-	if ($log && $log->GetEnabled()) {
-		$log->PrintHtmlHeader();
-		$log->PrintHtml();
-		$log->PrintHtmlFooter();
-	}
-	unset($controller);
-}
-
 ?>
 

--- /dev/null
+++ b/templates/debug.tpl
@@ -1,1 +1,41 @@
+{*
+ * Debug
+ *
+ * Debug log template
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2013 Christopher Han
+ * @packge GitPHP
+ * @subpackage Template
+ *}
+<table class"debug">
+<tbody>
+{foreach from=$debuglog->GetEntries() item=entry}
+  <tr>
+    <td class="debug_key">
+      {$entry.name|escape}
+    </td>
+    <td class="debug_value">
+      {if $entry.value}
+        {if strlen($entry.value) > 512}
+          {$entry.value|truncate:512:'...'|escape}
+          <br />
+          <span class="debug_addl">{strlen($entry.value)-512} bytes more in output</span>
+        {else}
+          {$entry.value|escape}
+        {/if}
+        <br />
+      {/if}
+      <span class="debug_toggle">trace</span>
+      <div class="debug_bt">{$entry.bt|escape}</div>
+    </td>
+    <td class="debug_time">
+      {if $entry.time}
+        {$entry.time*1000|string_format:"%.1f"} {if $entry.reltime}ms from start{else}ms{/if}
+      {/if}
+    </td>
+  </tr>
+{/foreach}
+</tbody>
+</table>
 

comments