summaryrefslogtreecommitdiff
path: root/app/Core/Template.php
diff options
context:
space:
mode:
authorFrédéric Guillot <fred@kanboard.net>2014-05-22 12:28:28 -0400
committerFrédéric Guillot <fred@kanboard.net>2014-05-22 12:28:28 -0400
commit2230dd4e6b148346c0ec596b9e3e12996a762ed8 (patch)
treeef99ccde4f8b18592a3fb06a6ec45162c501fe38 /app/Core/Template.php
parenta750b8ab2a0cb715da6fd9025a7ec8375db68a4d (diff)
Code refactoring (add autoloader and change files organization)
Diffstat (limited to 'app/Core/Template.php')
-rw-r--r--app/Core/Template.php72
1 files changed, 72 insertions, 0 deletions
diff --git a/app/Core/Template.php b/app/Core/Template.php
new file mode 100644
index 00000000..8740a685
--- /dev/null
+++ b/app/Core/Template.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Core;
+
+/**
+ * Template class
+ *
+ * @package core
+ * @author Frederic Guillot
+ */
+class Template
+{
+ /**
+ * Template path
+ *
+ * @var string
+ */
+ const PATH = 'app/Templates/';
+
+ /**
+ * Load a template
+ *
+ * Example:
+ *
+ * $template->load('template_name', ['bla' => 'value']);
+ *
+ * @access public
+ * @return string
+ */
+ public function load()
+ {
+ if (func_num_args() < 1 || func_num_args() > 2) {
+ die('Invalid template arguments');
+ }
+
+ if (! file_exists(self::PATH.func_get_arg(0).'.php')) {
+ die('Unable to load the template: "'.func_get_arg(0).'"');
+ }
+
+ if (func_num_args() === 2) {
+
+ if (! is_array(func_get_arg(1))) {
+ die('Template variables must be an array');
+ }
+
+ extract(func_get_arg(1));
+ }
+
+ ob_start();
+
+ include self::PATH.func_get_arg(0).'.php';
+
+ return ob_get_clean();
+ }
+
+ /**
+ * Render a page layout
+ *
+ * @access public
+ * @param string $template_name Template name
+ * @param array $template_args Key/value map
+ * @param string $layout_name Layout name
+ * @return string
+ */
+ public function layout($template_name, array $template_args = array(), $layout_name = 'layout')
+ {
+ return $this->load(
+ $layout_name,
+ $template_args + array('content_for_layout' => $this->load($template_name, $template_args))
+ );
+ }
+}