From 2a802ee5e1d5ef20d8cce9e8fedfb65e8aeae9c9 Mon Sep 17 00:00:00 2001 From: Gilles Crettenand Date: Tue, 2 Jun 2015 09:47:22 +0200 Subject: [PATCH] Use template for HTML --- Lib/Formatter/Html.php | 29 ++++++++++++++++++++++++----- template.html | 15 +++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 template.html diff --git a/Lib/Formatter/Html.php b/Lib/Formatter/Html.php index 1575a6b..f92c6cb 100644 --- a/Lib/Formatter/Html.php +++ b/Lib/Formatter/Html.php @@ -18,12 +18,14 @@ class Html extends Formatter { $first = reset($data); $single = ! is_array($first); - $content = ''; + $content = '
'; if($single) { $content .= ""; } else { + $columns = array_keys($first); + $content .= ''; - foreach(array_keys($first) as $k) { + foreach($columns as $k) { $content .= ""; } $content .= ''; @@ -36,8 +38,14 @@ class Html extends Formatter { } else { foreach($data as $row) { $content .= ''; - foreach($row as $col) { - $content .= ''; + foreach($columns as $c) { + $v = print_r(isset($row[$c]) ? $row[$c] : '', true); + + $limit = 50; + if(strlen($v) > $limit) { + $v = substr($v, 0, $limit).' [...]'; + } + $content .= ""; } $content .= ''; } @@ -75,6 +83,17 @@ class Html extends Formatter { $content = '

Unable to render this

'; } - echo $content; + $html = file_get_contents('template.html'); + + $context = array( + 'title' => 'BSR - WebService', + 'content' => $content + ); + + foreach($context as $k => $v) { + $html = str_replace("{{ $k }}", $v, $html); + } + + echo $html; } } diff --git a/template.html b/template.html new file mode 100644 index 0000000..210b161 --- /dev/null +++ b/template.html @@ -0,0 +1,15 @@ + + + + + BSR WebService - {{ title }} + + + + + {{ content }} + + + + + \ No newline at end of file
FieldValue
$k
'.print_r($col, true).'$v