From af4c9e77c85afec6fb3b878f945461d6f1f82a2e Mon Sep 17 00:00:00 2001 From: Gilles Crettenand Date: Tue, 2 Jun 2015 22:21:17 +0200 Subject: [PATCH] better templating --- Lib/Formatter/Html.php | 12 +++++++----- template.html => templates/layout.html | 9 +-------- templates/panel.html | 8 ++++++++ 3 files changed, 16 insertions(+), 13 deletions(-) rename template.html => templates/layout.html (84%) create mode 100644 templates/panel.html diff --git a/Lib/Formatter/Html.php b/Lib/Formatter/Html.php index 2d1270d..f9e61eb 100644 --- a/Lib/Formatter/Html.php +++ b/Lib/Formatter/Html.php @@ -97,12 +97,14 @@ class Html extends Formatter { ); } - $html = file_get_contents('template.html'); + $panel = static::template($context, 'panel'); + echo static::template(array('title' => $context['title'], 'content' => $panel)); + } - foreach($context as $k => $v) { - $html = str_replace("{{ $k }}", $v, $html); - } + public static function template(array $context = array(), $template = 'layout') { + $html = file_get_contents(sprintf('templates/%s.html', $template)); - echo $html; + $patterns = array_map(function($p) { return "{{ $p }}"; }, array_keys($context)); + return str_replace($patterns, array_values($context), $html); } } diff --git a/template.html b/templates/layout.html similarity index 84% rename from template.html rename to templates/layout.html index 3430264..55195f7 100644 --- a/template.html +++ b/templates/layout.html @@ -30,14 +30,7 @@
-
-
-

{{ title }}

-
-
- {{ content }} -
-
+ {{ content }}
diff --git a/templates/panel.html b/templates/panel.html new file mode 100644 index 0000000..abdf24c --- /dev/null +++ b/templates/panel.html @@ -0,0 +1,8 @@ +
+
+

{{ title }}

+
+
+ {{ content }} +
+
\ No newline at end of file