summaryrefslogtreecommitdiff
path: root/lib/template.php
blob: 09f9aa29669bd615a63dbd1bb3e6aa4c6e788758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)));
    }
}