diff options
author | Frédéric Guillot <contact@fredericguillot.com> | 2014-01-25 14:56:02 -0500 |
---|---|---|
committer | Frédéric Guillot <contact@fredericguillot.com> | 2014-01-25 14:56:02 -0500 |
commit | 9383a15af699ede77142d040b65118e15754a2ca (patch) | |
tree | b550b5adf5bcf8f5a8793c188cc5630f26a27d49 /lib/template.php |
First commit
Diffstat (limited to 'lib/template.php')
-rw-r--r-- | lib/template.php | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/template.php b/lib/template.php new file mode 100644 index 00000000..09f9aa29 --- /dev/null +++ b/lib/template.php @@ -0,0 +1,38 @@ +<?php + +class Template +{ + const PATH = 'templates/'; + + // Template\load('template_name', ['bla' => 'value']); + 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(); + } + + 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))); + } +} |